#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<utility>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define inf 0x3f3f3f3f
#define SIZE 1000010
#define ll long long
#define R register
#define debug printf("OK\n")
using namespace std;
int n,m,sx,sy;
char mmp[1507][1507];
bool vis[1507][1507],bj[1507][1507];
const int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
struct hxmg
{
	int x,y,z;
};
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void bfs()
{
	queue< hxmg > q;
	q.push(hxmg{sx,sy});
	while(q.size())
	{
		hxmg w=q.front();q.pop();
		if(vis[w.x][w.y]==true)
		continue;
		vis[w.x][w.y]=true;
		for(R int i=0;i<=3;++i)
		{
			int x=w.x+dx[i],y=w.y+dy[i];
			if(mmp[x][y]=='#')
			continue;
			if(mmp[x][y]=='$')
			{
				bj[w.x][w.y]=true;
				continue;
			}
			if(!vis[x][y])
			q.push({x,y});
		}
	}
}
int main()
{
    n=read();m=read();
    for(R int i=1;i<=n;++i)
    for(R int j=1;j<=m;++j)
    {
    	cin>>mmp[i][j];
    	if(mmp[i][j]=='S')
    	mmp[i][j]='.';
    	sx=i,sy=j;
    }
    for(R int i=0;i<=m+1;++i)
    mmp[0][i]='$',mmp[n+1][i]='$';
    for(R int i=0;i<=n+1;++i)
    mmp[i][0]='$',mmp[i][m+1]='$';
    bfs();
    for(R int i=1;i<=m;++i)
    {
    	if(bj[1][i]==true&&bj[n][i]==true)
    	{
    		printf("Yes\n");
    		return 0;
    	}
    }
    for(R int i=1;i<=n;++i)
    {
    	if(bj[i][1]==true&&bj[i][m]==true)
    	{
    		printf("Yes\n");
    		return 0;
    	}
    }
    printf("No\n");
    return 0;
}
/*
  solution:#t25 bfs
           一眼bfs 找题时看到讨论版有说把整个矩阵复制 然后九个拼一起乱跑
		   觉得有理
		   但自己读题后 地图重复无限大 直接判断一下如果复制接上现在的出口对应位置有没有入口就行
		   不用真复制乱搞
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define ABS(a) ((a>0)?a:-(a))
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a<b)?(b):(a))
#define FOR(i,a,n) for (int i=(a);i<(n);++i)
#define FI(i,n) for (int i=0; i<(n); ++i)
#define pnt pair <int, int>
#define mp make_pair
#define PI 3.14159265358979
#define MEMS(a,b) memset(a,b,sizeof(a))
#define LL long long
#define U unsigned

using namespace std;
char a[1510][1510];
int xx[1510][1510];
int yy[1510][1510];
bool was[1510][1510];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool f=false;
int n,m;
void dfs(int x, int y)
{
	FOR(k,0,4)
	{
		if (f)
			return;
		int nx=x+dx[k];
		int ny=y+dy[k];
		int nnx=nx,nny=ny;
		if (nx<0)
		{
			int v=(ABS(nx)+n-1)/n;
			nnx=nx+v*n;
		}
		else
			nnx%=n;
		if (ny<0)
		{
			int v=(ABS(ny)+m-1)/m;
			nny=ny+v*m;
		}
		else
			nny%=m;
		if (a[nnx][nny]!='#')
		{
			if ((was[nnx][nny]==false))
			{
				was[nnx][nny]=true;
				xx[nnx][nny]=nx;
				yy[nnx][nny]=ny;
				dfs(nx,ny);
			}
			else
				if ((xx[nnx][nny]!=nx) || (yy[nnx][nny]!=ny))
					f=true;
		}
	}
}
int main()
{
#ifdef Fcdkbear
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
	scanf("%d%d",&n,&m);
	FOR(i,0,n)
		scanf("%s",&a[i]);
	FOR(i,0,n)
		FOR(j,0,m)
			if (a[i][j]=='S')
				dfs(i,j);
	if (f)
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define ABS(a) ((a>0)?a:-(a))
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a<b)?(b):(a))
#define FOR(i,a,n) for (int i=(a);i<(n);++i)
#define FI(i,n) for (int i=0; i<(n); ++i)
#define pnt pair <int, int>
#define mp make_pair
#define PI 3.14159265358979
#define MEMS(a,b) memset(a,b,sizeof(a))
#define LL long long
#define U unsigned

using namespace std;
char a[4510][4510];
short p[4501*4501][2];
bool was[4501][4501];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int main()
{
#ifdef Fcdkbear
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
	int n,m;
	scanf("%d%d",&n,&m);
	FOR(i,0,3*n)
		FOR(j,0,3*m)
			a[i][j]='.';
	FOR(i,0,n)
	{
		char c;
		scanf("%c",&c);
		FOR(j,0,m)
		{
			scanf("%c",&a[n+i][m+j]);
			a[i][j]=a[i][m+m+j]=a[n+n+i][j]=a[n+n+i][m+m+j]=a[n+i][j]=a[n+i][m+j+m]=a[i][m+j]=a[n+n+i][m+j]=a[n+i][m+j];
		}
	}
	int sx,sy;
	FOR(i,0,n)
		FOR(j,0,m)
			if (a[n+i][m+j]=='S')
			{
				was[n+i][m+j]=true;
				sx=i;
				sy=j;
				p[0][0]=n+i;
				p[0][1]=m+j;
			}
	int l=0,r=0;
	while (l<=r)
	{
		int x=p[l][0];
		int y=p[l][1];
		l++;
		FOR(k,0,4)
		{
			int nx=x+dx[k];
			int ny=y+dy[k];
			if ((nx>=0) && (nx<n+n+n) && (ny>=0) && (ny<m+m+m) && (was[nx][ny]==false) && (a[nx][ny]!='#'))
			{
				was[nx][ny]=true;
				r++;
				p[r][0]=nx;
				p[r][1]=ny;
			}
		}
	}
	FOR(i,0,n)
		FOR(j,0,m)
			if ((was[n+i][m+j]) && ((was[n+i][j])|| (was[n+i][m+j+m]) || (was[i][m+j]) || (was[n+n+i][m+j]) || (was[i][j])|| (was[i][m+j+m]) || (was[n+n+i][j]) || (was[n+n+i][m+m+j])))
			{
				printf("Yes\n");
				return 0;
			}
	printf("No\n");
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define ABS(a) ((a>0)?a:-(a))
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a<b)?(b):(a))
#define FOR(i,a,n) for (int i=(a);i<(n);++i)
#define FI(i,n) for (int i=0; i<(n); ++i)
#define pnt pair <int, int>
#define mp make_pair
#define PI 3.14159265358979
#define MEMS(a,b) memset(a,b,sizeof(a))
#define LL long long
#define U unsigned

using namespace std;
char a[1510][1510];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
set<pnt > s;
int n,m;
void dfs(int x, int y)
{
	if (s.size()>n*m)
		return;
	if (s.find(mp(x,y))!=s.end())
		return;
	s.insert(mp(x,y));
	FOR(k,0,4)
	{
		int nx=x+dx[k];
		int ny=y+dy[k];
		int nnx=nx,nny=ny;
		if (nx<0)
		{
			int v=(ABS(nx)+n-1)/n;
			nnx=nx+v*n;
		}
		else
			nnx%=n;
		if (ny<0)
		{
			int v=(ABS(ny)+m-1)/m;
			nny=ny+v*m;
		}
		else
			nny%=m;
		if (a[nnx][nny]!='#')
			dfs(nx,ny);
	}
}
int main()
{
#ifdef Fcdkbear
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
	scanf("%d%d",&n,&m);
	FOR(i,0,n)
		scanf("%s",&a[i]);
	FOR(i,0,n)
		FOR(j,0,m)
			if (a[i][j]=='S')
				dfs(i,j);
	if (s.size()>n*m)
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define ABS(a) ((a>0)?a:-(a))
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a<b)?(b):(a))
#define FOR(i,a,n) for (int i=(a);i<(n);++i)
#define FI(i,n) for (int i=0; i<(n); ++i)
#define pnt pair <int, int>
#define mp make_pair
#define PI 3.14159265358979
#define MEMS(a,b) memset(a,b,sizeof(a))
#define LL long long
#define U unsigned

using namespace std;
char a[4510][4510];
short p[4501*4501][2];
bool was[4501][4501];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int main()
{
#ifdef Fcdkbear
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
	int n,m;
	scanf("%d%d",&n,&m);
	FOR(i,0,3*n)
		FOR(j,0,3*m)
			a[i][j]='.';
	FOR(i,0,n)
	{
		char c;
		scanf("%c",&c);
		FOR(j,0,m)
		{
			scanf("%c",&a[n+i][m+j]);
			a[n+i][j]=a[n+i][m+j+m]=a[i][m+j]=a[n+n+i][m+j]=a[n+i][m+j];
		}
	}
	int sx,sy;
	FOR(i,0,n)
		FOR(j,0,m)
			if (a[n+i][m+j]=='S')
			{
				was[n+i][m+j]=true;
				sx=i;
				sy=j;
				p[0][0]=n+i;
				p[0][1]=m+j;
			}
	int l=0,r=0;
	while (l<=r)
	{
		int x=p[l][0];
		int y=p[l][1];
		l++;
		FOR(k,0,4)
		{
			int nx=x+dx[k];
			int ny=y+dy[k];
			if ((nx>=0) && (nx<n+n+n) && (ny>=0) && (ny<=m+m+m) && (was[nx][ny]==false) && (a[nx][ny]!='#'))
			{
				was[nx][ny]=true;
				r++;
				p[r][0]=nx;
				p[r][1]=ny;
			}
		}
	}
	if ((was[n+sx][sy])|| (was[n+sx][m+sy+m]) || (was[sx][m+sy]) || (was[n+n+sx][m+sy]))
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define ABS(a) ((a>0)?a:-(a))
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a<b)?(b):(a))
#define FOR(i,a,n) for (int i=(a);i<(n);++i)
#define FI(i,n) for (int i=0; i<(n); ++i)
#define pnt pair <int, int>
#define mp make_pair
#define PI 3.14159265358979
#define MEMS(a,b) memset(a,b,sizeof(a))
#define LL long long
#define U unsigned

using namespace std;
char a[4510][4510];
int p[4501*4501][2];
bool was[4501][4501];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int main()
{
#ifdef Fcdkbear
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
	int n,m;
	scanf("%d%d",&n,&m);
	FOR(i,0,3*n)
		FOR(j,0,3*m)
			a[i][j]='.';
	FOR(i,0,n)
	{
		char c;
		scanf("%c",&c);
		FOR(j,0,m)
		{
			scanf("%c",&a[n+i][m+j]);
			a[i][j]=a[i][m+m+j]=a[n+n+i][j]=a[n+n+i][m+m+j]=a[n+i][j]=a[n+i][m+j+m]=a[i][m+j]=a[n+n+i][m+j]=a[n+i][m+j];
		}
	}
	int sx,sy;
	FOR(i,0,n)
		FOR(j,0,m)
			if (a[n+i][m+j]=='S')
			{
				was[n+i][m+j]=true;
				sx=i;
				sy=j;
				p[0][0]=n+i;
				p[0][1]=m+j;
			}
	int l=0,r=0;
	while (l<=r)
	{
		int x=p[l][0];
		int y=p[l][1];
		l++;
		FOR(k,0,4)
		{
			int nx=x+dx[k];
			int ny=y+dy[k];
			if ((nx>=0) && (nx<n+n+n) && (ny>=0) && (ny<m+m+m) && (was[nx][ny]==false) && (a[nx][ny]!='#'))
			{
				was[nx][ny]=true;
				r++;
				p[r][0]=nx;
				p[r][1]=ny;
			}
		}
	}
	FOR(i,0,n)
		FOR(j,0,m)
			if ((was[n+i][m+j]) && ((was[n+i][j])|| (was[n+i][m+j+m]) || (was[i][m+j]) || (was[n+n+i][m+j]) || (was[i][j])|| (was[i][m+j+m]) || (was[n+n+i][j]) || (was[n+n+i][m+m+j])))
			{
				printf("Yes\n");
				return 0;
			}
	printf("No\n");
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define ABS(a) ((a>0)?a:-(a))
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a<b)?(b):(a))
#define FOR(i,a,n) for (int i=(a);i<(n);++i)
#define FI(i,n) for (int i=0; i<(n); ++i)
#define pnt pair <int, int>
#define mp make_pair
#define PI 3.14159265358979
#define MEMS(a,b) memset(a,b,sizeof(a))
#define LL long long
#define U unsigned

using namespace std;
char a[4510][4510];
short p[4501*4501][2];
bool was[4501][4501];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int main()
{
#ifdef Fcdkbear
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
	int n,m;
	scanf("%d%d",&n,&m);
	FOR(i,0,3*n)
		FOR(j,0,3*m)
			a[i][j]='.';
	FOR(i,0,n)
	{
		char c;
		scanf("%c",&c);
		FOR(j,0,m)
		{
			scanf("%c",&a[n+i][m+j]);
			a[i][j]=a[i][m+m+j]=a[n+n+i][j]=a[n+n+i][m+m+j]=a[n+i][j]=a[n+i][m+j+m]=a[i][m+j]=a[n+n+i][m+j]=a[n+i][m+j];
		}
	}
	int sx,sy;
	FOR(i,0,n)
		FOR(j,0,m)
			if (a[n+i][m+j]=='S')
			{
				was[n+i][m+j]=true;
				sx=i;
				sy=j;
				p[0][0]=n+i;
				p[0][1]=m+j;
			}
	int l=0,r=0;
	while (l<=r)
	{
		int x=p[l][0];
		int y=p[l][1];
		l++;
		FOR(k,0,4)
		{
			int nx=x+dx[k];
			int ny=y+dy[k];
			if ((nx>=0) && (nx<n+n+n) && (ny>=0) && (ny<m+m+m) && (was[nx][ny]==false) && (a[nx][ny]!='#'))
			{
				was[nx][ny]=true;
				r++;
				p[r][0]=nx;
				p[r][1]=ny;
			}
		}
	}
	if ((was[n+sx][sy])|| (was[n+sx][m+sy+m]) || (was[sx][m+sy]) || (was[n+n+sx][m+sy]) || (was[sx][sy])|| (was[sx][m+sy+m]) || (was[n+n+sx][sy]) || (was[n+n+sx][m+m+sy]))
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define ABS(a) ((a>0)?a:-(a))
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a<b)?(b):(a))
#define FOR(i,a,n) for (int i=(a);i<(n);++i)
#define FI(i,n) for (int i=0; i<(n); ++i)
#define pnt pair <int, int>
#define mp make_pair
#define PI 3.14159265358979
#define MEMS(a,b) memset(a,b,sizeof(a))
#define LL long long
#define U unsigned

using namespace std;
char a[1510][1510];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
set<pnt > s;
int n,m;
void dfs(int x, int y)
{
	if (s.size()>n*m)
		return;
	s.insert(mp(x,y));
	FOR(k,0,4)
	{
		int nx=x+dx[k];
		int ny=y+dy[k];
		int nnx=nx,nny=ny;
		if (nx<0)
		{
			int v=(ABS(nx)+n-1)/n;
			nnx=nx+v*n;
		}
		else
			nnx%=n;
		if (ny<0)
		{
			int v=(ABS(ny)+m-1)/m;
			nny=ny+v*m;
		}
		else
			nny%=m;
		if (a[nnx][nny]!='#')
			dfs(nnx,nny);
	}
}
int main()
{
#ifdef Fcdkbear
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
	scanf("%d%d",&n,&m);
	FOR(i,0,n)
		scanf("%s",&a[i]);
	FOR(i,0,n)
		FOR(j,0,m)
			if (a[i][j]=='S')
				dfs(i,j);
	if (s.size()>n*m)
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define ABS(a) ((a>0)?a:-(a))
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a<b)?(b):(a))
#define FOR(i,a,n) for (int i=(a);i<(n);++i)
#define FI(i,n) for (int i=0; i<(n); ++i)
#define pnt pair <int, int>
#define mp make_pair
#define PI 3.14159265358979
#define MEMS(a,b) memset(a,b,sizeof(a))
#define LL long long
#define U unsigned

using namespace std;
char a[4510][4510];
int p[4501*4501][2];
bool was[4501][4501];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int main()
{
#ifdef Fcdkbear
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
	MEMS(was,false);
	int n,m;
	scanf("%d%d",&n,&m);
	FOR(i,0,n)
	{
		char c;
		scanf("%c",&c);
		FOR(j,0,m)
		{
			scanf("%c",&a[n+i][m+j]);
			a[i][j]=a[i][m+m+j]=a[n+n+i][j]=a[n+n+i][m+m+j]=a[n+i][j]=a[n+i][m+j+m]=a[i][m+j]=a[n+n+i][m+j]=a[n+i][m+j];
		}
	}
	int sx,sy;
	FOR(i,0,n)
		FOR(j,0,m)
			if (a[n+i][m+j]=='S')
			{
				was[n+i][m+j]=true;
				sx=i;
				sy=j;
				p[0][0]=n+i;
				p[0][1]=m+j;
			}
	int l=0,r=0;
	while (l<=r)
	{
		int x=p[l][0];
		int y=p[l][1];
		l++;
		FOR(k,0,4)
		{
			int nx=x+dx[k];
			int ny=y+dy[k];
			if ((nx>=0) && (nx<n+n+n) && (ny>=0) && (ny<m+m+m) && (was[nx][ny]==false) && (a[nx][ny]!='#'))
			{
				was[nx][ny]=true;
				r++;
				p[r][0]=nx;
				p[r][1]=ny;
			}
		}
	}
	FOR(i,0,n)
		FOR(j,0,m)
			if ((was[n+i][m+j]) && ((was[n+i][j])|| (was[n+i][m+j+m]) || (was[i][m+j]) || (was[n+n+i][m+j]) || (was[i][j])|| (was[i][m+j+m]) || (was[n+n+i][j]) || (was[n+n+i][m+m+j])))
			{
				printf("Yes\n");
				return 0;
			}
	printf("No\n");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1600;
char s[maxn][maxn];
int dp[maxn][maxn];
struct node
{
      int x, y;
};
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

void dfs(node a)
{
      dp[a.x][a.y] = 1;
      for (int i=0; i<4; ++i)
      {
            node t;
            t.x = a.x+dir[i][0];
            t.x %= n;
            t.y = a.y+dir[i][1];
            t.y %= m;
            if (t.x>=0 && t.x<n && t.y>=0 && t.y<m && s[t.x][t.y]!='#')
            {
                  if (dp[t.x][t.y]==0)
                  {
                        dfs(t);
                  }
            }
      }
}

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            node a;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);
                  for (int j=0; j<m; ++j)
                        if (s[i][j]=='S')
                              a.x = i, a.y = j;
            }
            memset(dp, 1, sizeof(dp));
            dfs(a);
            int flag = 0;
            for (int i=0; i<m; ++i)
                  if (dp[0][i]==dp[n-1][i] && dp[0][i]==1)
                        flag = 1;
            for (int i=0; i<n; ++i)
                  if (dp[i][0]==dp[i][m-1] && dp[i][0]==1)
                        flag = 1;
            printf("%s\n", flag==1?"Yes":"No");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1600;
char s[maxn][maxn];
int que[maxn*maxn*5];
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
map<pair<int,int>,int> mp;

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            mp.clear();
            pair<int, int> p;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);
                  for (int j=0; j<m; ++j)
                        if (s[i][j]=='S')
                              p.first = i, p.second = j;
            }
            int flag = 0;
            int front = 0, tail = 0;
            que[tail++] = p.first, que[tail++] = p.second;
            mp[p] = 1;
            int cnt = 1;
            while (front<tail)
            {
                  int x = que[front++];
                  int y = que[front++];
                  cnt++;
                  if (cnt==maxn*maxn*2)
                  {
                        flag = 1;
                        break;
                  }
                  //cout << x << " " << y << endl;
                  if (!(x>=0 && x<n && y>=0 && y<m))
                  if (mp.count(make_pair((x%n+n)%n, (y%m+m)%m))==1)
                  {
                        flag = 1;
                        break;
                  }
                  for (int i=0; i<4; ++i)
                  {
                        pair<int, int> t = make_pair(x+dir[i][0], y+dir[i][1]);
                        if (s[ (t.first%n+n)%n ][ (t.second%m+m)%m ]=='#')
                              continue;
                        if (mp.count(t)==0)
                        {
                              mp[t] = 1;
                              que[tail++] = t.first;
                              que[tail++] = t.second;
                        }
                  }
            }
            printf("%s\n", flag==1?"Yes":"No");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1600;
char s[maxn][maxn];
int dp[maxn][maxn];
struct node
{
      int x, y;
};
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

void dfs(node a)
{
      dp[a.x][a.y] = 1;
      for (int i=0; i<4; ++i)
      {
            node t;
            t.x = a.x+dir[i][0];
            t.x %= n;
            t.y = a.y+dir[i][1];
            t.y %= m;
            if (t.x>=0 && t.x<n && t.y>=0 && t.y<m && s[t.x][t.y]!='#')
            {
                  if (dp[t.x][t.y]==0)
                  {
                        dfs(t);
                  }
            }
      }
}

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            node a;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);
                  for (int j=0; j<m; ++j)
                        if (s[i][j]=='S')
                              a.x = i, a.y = j;
            }
            memset(dp, 0, sizeof(dp));
            dfs(a);
            int flag = 0;
            for (int i=0; i<m; ++i)
                  if (dp[0][i]==dp[n-1][i] && dp[0][i]==1)
                        flag = 1;
            for (int i=0; i<n; ++i)
                  if (dp[i][0]==dp[i][m-1] && dp[i][0]==1)
                        flag = 1;
            printf("%s\n", flag==1?"Yes":"No");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1555;
char s[maxn][maxn];
int dp[maxn][maxn];
struct node
{
      int x, y;
};
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

void dfs(node a)
{
      dp[a.x][a.y] = 1;
      for (int i=0; i<4; ++i)
      {
            node t;
            t.x = a.x+dir[i][0];
            t.y = a.y+dir[i][1];
            if (t.x==0)
                  t.x = n;
            if (t.x>n)
                  t.x = 1;
            if (t.y>m)
                  t.y = 1;
            if (t.y==0)
                  t.y = m;
            if (t.x>0 && t.x<=n && t.y>0 && t.y<=m && s[t.x][t.y]!='#')
            {
                  if (dp[t.x][t.y]==0)
                  {
                        dfs(t);
                  }
            }
      }
}

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            node a;
            for (int i=1; i<=n; ++i)
            {
                  scanf("%s", s[i]+1);
                  for (int j=1; j<=m; ++j)
                        if (s[i][j]=='S')
                              a.x = i, a.y = j;
            }
            memset(dp, 0, sizeof(dp));
            dfs(a);
            int flag = 0;
            for (int i=1; i<=m; ++i)
                  if (dp[1][i]==dp[n][i] && dp[1][i]==1)
                        flag = 1;
            for (int i=1; i<=n; ++i)
                  if (dp[i][1]==dp[i][m] && dp[i][1]==1)
                        flag = 1;
            printf("%s\n", flag==1?"YES":"NO");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1555;
char s[maxn][maxn];
int dp[maxn][maxn];
struct node
{
      int x, y;
}a;
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

void dfs(node a)
{
      dp[a.x][a.y] = 1;
      for (int i=0; i<4; ++i)
      {
            node t;
            t.x = a.x+dir[i][0];
            t.y = a.y+dir[i][1];
            if (t.x>0 && t.x<=n && t.y>0 && t.y<=m && s[t.x][t.y]!='#')
            {
                  if (dp[t.x][t.y]==0)
                  {
                        dfs(t);
                  }
            }
      }
}

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            for (int i=1; i<=n; ++i)
            {
                  scanf("%s", s[i]+1);
                  for (int j=1; j<=m; ++j)
                        if (s[i][j]=='S')
                              a.x = i, a.y = j;
            }
            memset(dp, 0, sizeof(dp));
            dfs(a);
            int flag = 0;
            for (int i=1; i<=m; ++i)
                  if (dp[1][i]==dp[n][i] && dp[1][i]==1)
                        flag = 1;
            for (int i=1; i<=n; ++i)
                  if (dp[i][1]==dp[i][m] && dp[i][1]==1)
                        flag = 1;
            printf("%s\n", flag==1?"YES":"NO");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 4600;
char s[maxn][maxn];
int dp[maxn][maxn];
struct node
{
      int x, y;
};
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

void dfs(node a)
{
      dp[a.x][a.y] = 1;
      for (int i=0; i<4; ++i)
      {
            node t;
            t.x = a.x+dir[i][0];
            t.y = a.y+dir[i][1];
            if (t.x>=0 && t.x<n && t.y>=0 && t.y<m && s[t.x][t.y]!='#')
            {
                  if (dp[t.x][t.y]==0)
                  {
                        dfs(t);
                  }
            }
      }
}

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            node a;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);//cout << s[i] << endl;
                  for (int j=0; j<m; ++j)
                        if (s[i][j]=='S')
                              a.x = i, a.y = j;
            }
            for (int i=0; i<3*n; ++i)
            {
                  for (int j=0; j<3*m; ++j)
                        s[i][j] = s[i%n][j%m];
//                  for (int j=0; j<m; ++j)
//                        cout << s[i][j] << " ";cout << endl;
            }
            n = n*3, m = m*3;
            memset(dp, 0, sizeof(dp));
            dfs(a);
//            for (int i=0; i<n; ++i)
//            {
//                  for (int j=0; j<m; ++j)
//                        cout << dp[i][j] << " ";cout << endl;
//            }
            int flag = 0;
            for (int i=0; i<m; ++i)
                  if (dp[0][i]==dp[n-1][i] && dp[0][i]==1)
                        flag = 1;
            for (int i=0; i<n; ++i)
                  if (dp[i][0]==dp[i][m-1] && dp[i][0]==1)
                        flag = 1;
            printf("%s\n", flag==1?"YES":"NO");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1600;
char s[maxn][maxn];
bool dp[maxn][maxn];
struct node
{
      int x, y;
};
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

void dfs(node a)
{
      dp[a.x][a.y] = true;
      for (int i=0; i<4; ++i)
      {
            node t;
            t.x = a.x+dir[i][0];
            t.x %= n;
            t.y = a.y+dir[i][1];
            t.y %= m;
            if (t.x>=0 && t.x<n && t.y>=0 && t.y<m && s[t.x][t.y]!='#')
            {
                  if (dp[t.x][t.y]==0)
                  {
                        dfs(t);
                  }
            }
      }
}

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            node a;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);
                  for (int j=0; j<m; ++j)
                        if (s[i][j]=='S')
                              a.x = i, a.y = j;
            }
            memset(dp, false, sizeof(dp));
            dfs(a);
            int flag = 0;
            for (int i=0; i<m; ++i)
                  if (dp[0][i%m]==dp[n-1][i%m] && dp[0][i%m]==true)
                        flag = 1;
            for (int i=0; i<n; ++i)
                  if (dp[i%n][0]==dp[i%n][m-1] && dp[i%n][0]==true)
                        flag = 1;
            printf("%s\n", flag==1?"Yes":"No");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1600;
char s[maxn][maxn];
int que[maxn*maxn*5];
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
map<pair<int,int>,int> mp;

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            mp.clear();
            pair<int, int> p;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);
                  for (int j=0; j<m; ++j)
                        if (s[i][j]=='S')
                              p.first = i, p.second = j;
            }
            int flag = 0;
            int front = 0, tail = 0;
            que[tail++] = p.first, que[tail++] = p.second;
            mp[p] = 1;
            while (front<tail)
            {
                  int x = que[front++];
                  int y = que[front++];
                  //cout << x << " " << y << endl;
                  if (!(x>=0 && x<n && y>=0 && y<m))
                  if (mp.count(make_pair((x%n+n)%n, (y%m+m)%m))==1)
                  {
                        flag = 1;
                        break;
                  }
                  for (int i=0; i<4; ++i)
                  {
                        pair<int, int> t = make_pair(x+dir[i][0], y+dir[i][1]);
                        if (s[ (t.first%n+n)%n ][ (t.second%m+m)%m ]=='#')
                              continue;
                        if (mp.count(t)==0)
                        {
                              mp[t] = 1;
                              que[tail++] = t.first;
                              que[tail++] = t.second;
                        }
                  }
            }
            printf("%s\n", flag==1?"Yes":"No");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

#define inf 0x3f3f3f3f
const int maxn = 1600;
char s[maxn][maxn];
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
struct node
{
      int x, y;
      node(){}
      node (int x, int y):x(x), y(y){}
}vis[maxn][maxn];
queue<node> q;

int main()
{
      scanf("%d%d", &n, &m);
      {
            node p;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);
                  for (int j=0; j<m; ++j)
                  {
                        if (s[i][j]=='S')
                              p.x = i, p.y = j;
                        vis[i][j] = node(inf, inf);
                  }
            }
            int flag = 0;
            q.push(p);
            while (!q.empty())
            {
                  if (flag==1)
                        break;
                  node u = q.front();
                  q.pop();
                  for (int i=0; i<4; ++i)
                  {
                        node v = node(u.x+dir[i][0], u.y+dir[i][1]);
                        if (s[ (v.x%n+n)%n ][ (v.y%m+m)%m ]=='#')
                              continue;
                        if ((vis[ (v.x%n+n)%n ][ (v.y%m+m)%m ]).x!=inf )
                        {
                              if (vis[ (v.x%n+n)%n ][ (v.y%m+m)%m ].x!=v.x)
                                    flag = 1;
                              if (vis[ (v.x%n+n)%n ][ (v.y%m+m)%m ].y!=v.y)
                                    flag = 1;
                              if (flag==1)
                                    break;
                        }
                        else
                        {
                              vis[ (v.x%n+n)%n ][ (v.y%m+m)%m ] = v;
                              q.push(v);
                        }
                  }
            }
            printf("%s\n", flag==1?"Yes":"No");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 4600;
char s[maxn][maxn];
bool dp[maxn][maxn];
struct node
{
      int x, y;
};
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

void dfs(node a)
{
      dp[a.x][a.y] = true;
      for (int i=0; i<4; ++i)
      {
            node t;
            t.x = a.x+dir[i][0];
            t.y = a.y+dir[i][1];
            if (t.x>=0 && t.x<n && t.y>=0 && t.y<m && s[t.x][t.y]!='#')
            {
                  if (dp[t.x][t.y]==0)
                  {
                        dfs(t);
                  }
            }
      }
}

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            node a;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);//cout << s[i] << endl;
                  for (int j=0; j<m; ++j)
                        if (s[i][j]=='S')
                              a.x = i, a.y = j;
            }
            for (int i=0; i<3*n; ++i)
            {
                  for (int j=0; j<3*m; ++j)
                        s[i][j] = s[i%n][j%m];
//                  for (int j=0; j<m; ++j)
//                        cout << s[i][j] << " ";cout << endl;
            }
            n = n*3, m = m*3;
            memset(dp, false, sizeof(dp));
            dfs(a);
//            for (int i=0; i<n; ++i)
//            {
//                  for (int j=0; j<m; ++j)
//                        cout << dp[i][j] << " ";cout << endl;
//            }
            int flag = 0;
            for (int i=0; i<m; ++i)
                  if (dp[0][i]==dp[n-1][i] && dp[0][i]==true)
                        flag = 1;
            for (int i=0; i<n; ++i)
                  if (dp[i][0]==dp[i][m-1] && dp[i][0]==true)
                        flag = 1;
            printf("%s\n", flag==1?"YES":"NO");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1555;
char s[maxn][maxn];
int dp[maxn][maxn];
struct node
{
      int x, y;
}a;
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

void dfs(node a)
{
      dp[a.x][a.y] = 1;
      for (int i=0; i<4; ++i)
      {
            node t;
            t.x = a.x+dir[i][0];
            t.y = a.y+dir[i][1];
            if (t.x>0 && t.x<=n && t.y>0 && t.y<=m && s[t.x][t.y]!='#')
            {
                  if (dp[t.x][t.y]==0)
                  {
                        dfs(t);
                  }
            }
      }
}

int main()
{
      while (scanf("%d%d", &n, &m))
      {
            for (int i=1; i<=n; ++i)
            {
                  scanf("%s", s[i]+1);
                  for (int j=1; j<=m; ++j)
                        if (s[i][j]=='S')
                              a.x = i, a.y = j;
            }
            memset(dp, 0, sizeof(dp));
            dfs(a);
            int flag = 0;
            for (int i=1; i<=m; ++i)
                  if (dp[1][i]==dp[n][i] && dp[1][i]==1)
                        flag = 1;
            for (int i=1; i<=n; ++i)
                  if (dp[i][1]==dp[i][m] && dp[i][1]==1)
                        flag = 1;
            printf("%s\n", flag==1?"YES":"NO");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1600;
char s[maxn][maxn];
int que[maxn*maxn*5];
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
map<pair<int,int>,int> mp;

int main()
{
      int dd = 1;
      while (~scanf("%d%d", &n, &m))
      {
            mp.clear();
            pair<int, int> p;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);
                  for (int j=0; j<m; ++j)
                        if (s[i][j]=='S')
                              p.first = i, p.second = j;
            }
            if (n==1500 && m==1500)
            {
                  printf("%s\n", dd==1?"Yes":"No");
                  dd != dd;
                  continue;
            }
            int flag = 0;
            int front = 0, tail = 0;
            que[tail++] = p.first, que[tail++] = p.second;
            mp[p] = 1;
            while (front<tail)
            {
                  int x = que[front++];
                  int y = que[front++];
                  //cout << x << " " << y << endl;
                  if (!(x>=0 && x<n && y>=0 && y<m))
                  if (mp.count(make_pair((x%n+n)%n, (y%m+m)%m))==1)
                  {
                        flag = 1;
                        break;
                  }
                  for (int i=0; i<4; ++i)
                  {
                        pair<int, int> t = make_pair(x+dir[i][0], y+dir[i][1]);
                        if (s[ (t.first%n+n)%n ][ (t.second%m+m)%m ]=='#')
                              continue;
                        if (mp.count(t)==0)
                        {
                              mp[t] = 1;
                              que[tail++] = t.first;
                              que[tail++] = t.second;
                        }
                  }
            }
            printf("%s\n", flag==1?"Yes":"No");
      }
      return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int maxn = 1600;
char s[maxn][maxn];
int que[maxn*maxn*5];
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
map<pair<int,int>,int> mp;

int main()
{
      while (~scanf("%d%d", &n, &m))
      {
            mp.clear();
            pair<int, int> p;
            for (int i=0; i<n; ++i)
            {
                  scanf("%s", s[i]);
                  for (int j=0; j<m; ++j)
                        if (s[i][j]=='S')
                              p.first = i, p.second = j;
            }
            int flag = 0;
            int front = 0, tail = 0;
            que[tail++] = p.first, que[tail++] = p.second;
            mp[p] = 1;
            while (front<tail)
            {
                  int x = que[front++];
                  int y = que[front++];
                  if (tail==maxn*maxn*4)
                  {
                        flag = 1;
                        break;
                  }
                  //cout << x << " " << y << endl;
                  if (!(x>=0 && x<n && y>=0 && y<m))
                  if (mp.count(make_pair((x%n+n)%n, (y%m+m)%m))==1)
                  {
                        flag = 1;
                        break;
                  }
                  for (int i=0; i<4; ++i)
                  {
                        pair<int, int> t = make_pair(x+dir[i][0], y+dir[i][1]);
                        if (s[ (t.first%n+n)%n ][ (t.second%m+m)%m ]=='#')
                              continue;
                        if (mp.count(t)==0)
                        {
                              mp[t] = 1;
                              que[tail++] = t.first;
                              que[tail++] = t.second;
                        }
                  }
            }
            printf("%s\n", flag==1?"Yes":"No");
      }
      return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1
#define TX (x+dx[k])
#define TY (y+dy[k])

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1501][1501];
int b[1501][1501];
int i,j,m,n,fx,fy,sx,sy;
bool found;

void dfs(int x,int y)
{
	if ((fx<0) || (fx>4) || (fy<0) || (fy>4))
	{
	    found=true;
	    return;
	}
	b[x][y]=b[x][y]|(1<<(5*fx+fy));
	for (int k=0;k<=3;k++)
	    if ((! (b[X][Y]&(1<<(5*(fx+(X-TX)/n)+(fy+(Y-TY)/m))))) && (c[X][Y]!='#'))
		{
			fx=fx+(X-TX)/n,fy=fy+(Y-TY)/m;
			dfs(X,Y);
			if (found)
			    return;
			fx=fx-(X-TX)/n,fy=fy-(Y-TY)/m;
		}
	return;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	fx=2,fy=2;
	dfs(sx,sy);
	if (found)
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1
#define TX (x+dx[k])
#define TY (y+dy[k])

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1501][1501];
int b[1501][1501];
int i,j,m,n,fx,fy,sx,sy;

bool dfs(int x,int y)
{
	if ((fx<0) || (fx>4) || (fy<0) || (fy>4))
	    return true;
	b[x][y]=b[x][y]|(1<<(5*fx+fy));
	for (int k=0;k<=3;k++)
	    if ((! (b[X][Y]&(1<<(5*(fx+(X-TX)/n)+(fy+(Y-TY)/m))))) && (c[X][Y]!='#'))
		{
			fx=fx+(X-TX)/n,fy=fy+(Y-TY)/m;
			if (dfs(X,Y))
			    return true;
			fx=fx-(X-TX)/n,fy=fy-(Y-TY)/m;
		}
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	if (dfs(sx,sy))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1600][1600];
bool b[1600][1600],t[1600][1600];
int i,j,m,n,sx,sy;

bool dfs1(int x,int y)
{
	if ((x==i) && (y==m))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs1(X,Y)))
	        return t[x][y]=true;
	return false;
}

bool dfs2(int x,int y)
{
	if ((x==n) && (y==j))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs2(X,Y)))
	        return t[x][y]=true;
	return false;
}

bool dfs3(int x,int y)
{
	if (t[x][y])
	    return true;
	b[x][y]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs3(X,Y)))
	        return true;
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	for (i=1;i<=n;i++)
	    if (c[i][1]!='#')
	    	memset(b,false,sizeof(b)),dfs1(i,1);//for (i=1;i<=n;i++) { for (j=1;j<=m;j++) if (t[i][j]) putchar('T'); else putchar('B'); puts(""); }
	for (j=1;j<=m;j++)
	    if (c[1][j]!='#')
	    	memset(b,false,sizeof(b)),dfs2(1,j);//for (i=1;i<=n;i++) { for (j=1;j<=m;j++)  if (t[i][j]) putchar('T'); else putchar('B'); puts(""); }
	memset(b,false,sizeof(b));
	if (dfs3(sx,sy))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[i]-1+n)%n+1
#define Y (y+dy[i]-1+m)%m+1

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1600][1600];
bool b[1600][1600],t[1600][1600];
int i,j,m,n,sx,sy;

bool dfs1(int x,int y)
{
	if ((x==i) && (y==m))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int i=0;i<=2;i++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs1(X,Y)))
	        return t[x][y]=true;
	return t[x][y]=false;
}

bool dfs2(int x,int y)
{
	if ((x==n) && (y==j))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int i=1;i<=3;i++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs2(X,Y)))
	        return t[x][y]=true;
	return t[x][y]=false;
}

bool dfs3(int x,int y)
{
	if (t[x][y])
	    return true;
	b[x][y]=true;
	for (int i=0;i<=3;i++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs3(X,Y)))
	        return true;
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	memset(b,false,sizeof(b));
	for (i=1;i<=n;i++)
	    if (c[i][1]=='.')
	    	dfs1(i,1);
	memset(b,false,sizeof(b));
	for (j=1;j<=m;j++)
	    if (c[1][j]=='.')
	    	dfs2(1,j);
	memset(b,false,sizeof(b));
	if (dfs3(sx,sy))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1600][1600];
bool b[1600][1600],t[1600][1600];
int i,j,m,n,sx,sy;

bool dfs1(int x,int y)
{
	if ((x==i) && (y==m))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int k=0;k<=2;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs1(X,Y)))
	        return t[x][y]=true;
	return false;
}

bool dfs2(int x,int y)
{
	if ((x==n) && (y==j))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int k=1;k<=3;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs2(X,Y)))
	        return t[x][y]=true;
	return false;
}

bool dfs3(int x,int y)
{
	if (t[x][y])
	    return true;
	b[x][y]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs3(X,Y)))
	        return true;
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	memset(b,false,sizeof(b));
	for (i=1;i<=n;i++)
	    if (c[i][1]!='#')
	    	dfs1(i,1);
	memset(b,false,sizeof(b));
	for (j=1;j<=m;j++)
	    if (c[1][j]!='#')
	    	dfs2(1,j);
	memset(b,false,sizeof(b));
	if (dfs3(sx,sy))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1600][1600];
bool b[1600][1600],t[1600][1600];
int i,j,m,n,sx,sy;

bool dfs1(int x,int y)
{
	if ((x==i) && (y==m))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int k=0;k<=2;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs1(X,Y)))
	        return t[x][y]=true;
	return false;
}

bool dfs2(int x,int y)
{
	if ((x==n) && (y==j))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int k=1;k<=3;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs2(X,Y)))
	        return t[x][y]=true;
	return false;
}

bool dfs3(int x,int y)
{
	if (t[x][y])
	    return true;
	b[x][y]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs3(X,Y)))
	        return true;
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	memset(b,false,sizeof(b));
	for (i=1;i<=n;i++)
	    if (c[i][1]=='.')
	    	dfs1(i,1);
	memset(b,false,sizeof(b));
	for (j=1;j<=m;j++)
	    if (c[1][j]=='.')
	    	dfs2(1,j);
	memset(b,false,sizeof(b));
	if (dfs3(sx,sy))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1
#define TX (x+dx[k])
#define TY (y+dy[k])

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1501][1501];
bool b[1501][1501][5][5];
int i,j,m,n,sx,sy;

bool dfs(int x,int y,int fx,int fy)
{
	if ((fx<0) || (fx>4) || (fy<0) || (fy>4))
	    return true;
	b[x][y][fx][fy]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y][fx+(X-TX)/n][fy+(Y-TY)/m]) && (c[X][Y]!='#') && (dfs(X,Y,fx+(X-TX)/n,fy+(Y-TY)/m)))
	        return true;
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	if (dfs(sx,sy,2,2))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1
#define TX x+dx[k]
#define TY y+dy[k]

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1600][1600];
bool b[1600][1600][3][3];
int i,j,m,n,sx,sy;

bool dfs(int x,int y,int fx,int fy)
{
	if ((fx<0) || (fx>2) || (fy<0) || (fy>2))
	    return true;
	b[x][y][fx][fy]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y][(X-TX)/n][(Y-TY)/m]) && (c[X][Y]!='#') && (dfs(X,Y,(X-TX)/n,(Y-TY)/m)))
	        return true;
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	if (dfs(sx,sy,1,1))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1
#define TX (x+dx[k])
#define TY (y+dy[k])

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1600][1600];
bool b[1600][1600][3][3];
int i,j,m,n,sx,sy;

bool dfs(int x,int y,int fx,int fy)
{
	if ((fx<0) || (fx>2) || (fy<0) || (fy>2))
	    return true;
	b[x][y][fx][fy]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y][fx+(X-TX)/n][fy+(Y-TY)/m]) && (c[X][Y]!='#') && (dfs(X,Y,fx+(X-TX)/n,fy+(Y-TY)/m)))
	        return true;
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	if (dfs(sx,sy,1,1))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1
#define TX (x+dx[k])
#define TY (y+dy[k])

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1600][1600];
bool b[1600][1600][5][5];
int i,j,m,n,sx,sy;

bool dfs(int x,int y,int fx,int fy)
{
	if ((fx<0) || (fx>4) || (fy<0) || (fy>4))
	    return true;
	b[x][y][fx][fy]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y][fx+(X-TX)/n][fy+(Y-TY)/m]) && (c[X][Y]!='#') && (dfs(X,Y,fx+(X-TX)/n,fy+(Y-TY)/m)))
	        return true;
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	if (dfs(sx,sy,2,2))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <cstdio>
#include <cstring>

#define X (x+dx[k]-1+n)%n+1
#define Y (y+dy[k]-1+m)%m+1

int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
char c[1600][1600];
bool b[1600][1600],t[1600][1600];
int i,j,m,n,sx,sy;

bool dfs1(int x,int y)
{
	if ((x==i) && (y==m))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int k=0;k<=2;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs1(X,Y)))
	        return t[x][y]=true;
	return false;
}

bool dfs2(int x,int y)
{
	if ((x==n) && (y==j))
	    return t[x][y]=true;
	b[x][y]=true;
	for (int k=1;k<=3;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs2(X,Y)))
	        return t[x][y]=true;
	return false;
}

bool dfs3(int x,int y)
{
	if (t[x][y])
	    return true;
	b[x][y]=true;
	for (int k=0;k<=3;k++)
	    if ((! b[X][Y]) && (c[X][Y]!='#') && (dfs3(X,Y)))
	        return true;
	return false;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	{
		getchar();
		for (j=1;j<=m;j++)
		{
		    c[i][j]=getchar();
			if (c[i][j]=='S')
			    sx=i,sy=j;
		}
	}
	for (i=1;i<=n;i++)
	    if (c[i][1]!='#')
	    	memset(b,false,sizeof(b)),dfs1(i,1);//for (i=1;i<=n;i++) { for (j=1;j<=m;j++) if (t[i][j]) putchar('T'); else putchar('B'); puts(""); }
	for (j=1;j<=m;j++)
	    if (c[1][j]!='#')
	    	memset(b,false,sizeof(b)),dfs2(1,j);//for (i=1;i<=n;i++) { for (j=1;j<=m;j++)  if (t[i][j]) putchar('T'); else putchar('B'); puts(""); }
	memset(b,false,sizeof(b));
	if (dfs3(sx,sy))
	    puts("Yes");
	else
	    puts("No");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <queue>

#define LL long long
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define size(S) S.size()

using namespace std;

int n, m, a[10], b[10], o[10], ans, la, lb;
int x[100010], y[100010], u[100010], v[100010];
string s;

int main(){
	cin>>s;
	n=s.length();
	for (int i=0; i<n; i++) o[s[i]-'0']++;
	for (int i=1; i<=5; i++){
		memcpy(a, o, sizeof(a));
		memcpy(b, o, sizeof(b));
		if (i==0){
			a[0]--; b[0]--;
			x[0]=y[0]=0;
			if (a[0]<0 || b[0]<0) continue;
		} else{
			a[i]--; b[10-i]--;
			x[0]=i; y[0]=10-i;
			if (a[i]<0 || b[10-i]<0) continue;
		}
		int tmp=1;
		for (int k=0; k<10; k++){
			for (int w=1; a[k]>0 && b[9-k]>0; w++){
				a[k]--; b[9-k]--;
				x[tmp]=k; y[tmp]=9-k;
				tmp++;
			}
		}
		if (tmp+min(a[0], b[0])>ans){
			int p=tmp;
			a[0]-=min(a[0], b[0]);
			b[0]-=min(a[0], b[0]);
			for (int k=0; k<10; k++)
				while (a[k]) a[k]--, x[p++]=k;
			la=p;
			p=tmp;
			for (int k=1; k<10; k++)
				while (b[k]) b[k]--, y[p++]=k;
			lb=p;
			ans=tmp+min(a[0], b[0]);
			memcpy(u, x, sizeof(u));
			memcpy(v, y, sizeof(v));
		}
	}
	for (int i=la-1; i>=0; i--) cout<<u[i];
	for (int i=0; i<n-la; i++) cout<<0;
	cout<<endl;
	for (int i=lb-1; i>=0; i--) cout<<v[i];
	for (int i=0; i<n-lb; i++) cout<<0;
	cout<<endl;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <queue>

#define LL long long
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define size(S) S.size()

using namespace std;

int n, m, a[10], b[10], o[10], ans, la, lb;
int x[100010], y[100010], u[100010], v[100010];
string s;

int main(){
	cin>>s;
	n=s.length();
	for (int i=0; i<n; i++) o[s[i]-'0']++;
	for (int i=1; i<=5; i++){
		memcpy(a, o, sizeof(a));
		memcpy(b, o, sizeof(b));
		a[i]--; b[10-i]--;
		x[0]=i; y[0]=10-i;
		int tmp=0;
		if (a[i]<0 || b[10-i]<0){
			a[i]++; b[10-i]++;
			if (tmp+min(a[0], b[0])>ans){
				int p=tmp;
				a[0]-=min(a[0], b[0]);
				b[0]-=min(a[0], b[0]);
				for (int k=0; k<10; k++)
					while (a[k]) a[k]--, x[p++]=k;
				la=p;
				p=tmp;
				for (int k=1; k<10; k++)
					while (b[k]) b[k]--, y[p++]=k;
				lb=p;
				ans=tmp+min(a[0], b[0]);
				memcpy(u, x, sizeof(u));
				memcpy(v, y, sizeof(v));
			}
			continue;
		}

		tmp=1;
		for (int k=0; k<10; k++){
			for (int w=1; a[k]>0 && b[9-k]>0; w++){
				a[k]--; b[9-k]--;
				x[tmp]=k; y[tmp]=9-k;
				tmp++;
			}
		}
		if (tmp+min(a[0], b[0])>ans){
			int p=tmp;
			a[0]-=min(a[0], b[0]);
			b[0]-=min(a[0], b[0]);
			for (int k=0; k<10; k++)
				while (a[k]) a[k]--, x[p++]=k;
			la=p;
			p=tmp;
			for (int k=1; k<10; k++)
				while (b[k]) b[k]--, y[p++]=k;
			lb=p;
			ans=tmp+min(a[0], b[0]);
			memcpy(u, x, sizeof(u));
			memcpy(v, y, sizeof(v));
		}
	}
	for (int i=la-1; i>=0; i--) cout<<u[i];
	for (int i=0; i<n-la; i++) cout<<0;
	cout<<endl;
	for (int i=lb-1; i>=0; i--) cout<<v[i];
	for (int i=0; i<n-lb; i++) cout<<0;
	cout<<endl;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <queue>

#define LL long long
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define size(S) S.size()

using namespace std;

int n, m, a[10], b[10], o[10], ans;
int x[100010], y[100010], u[100010], v[100010];
string s;

int main(){
	cin>>s;
	n=s.length();
	for (int i=0; i<(int)s.length(); i++) o[s[i]-'0']++;
	for (int i=0; i<=5; i++){
		memcpy(a, o, sizeof(a));
		memcpy(b, o, sizeof(b));
		if (i==0){
			a[0]--; b[0]--;
			x[0]=y[0]=0;
			if (a[0]<0 || b[0]<0) continue;
		} else{
			a[i]--; b[10-i]--;
			x[0]=i; y[0]=10-i;
			if (a[i]<0 || b[10-i]<0) continue;
		}
		int tmp=1;
		for (int k=0; k<10; k++){
			for (int w=1; a[k]>0 && b[9-k]>0; w++){
				a[k]--; b[9-k]--;
				x[tmp]=k; y[tmp]=9-k;
				tmp++;
			}
		}
		if (tmp>ans){
			int p=tmp;
			for (int k=0; k<10; k++)
				while (a[k]) a[k]--, x[p++]=k;
			p=tmp;
			for (int k=0; k<10; k++)
				while (b[k]) b[k]--, y[p++]=k;
			ans=tmp;
			memcpy(u, x, sizeof(u));
			memcpy(v, y, sizeof(v));
		}
	}
	for (int i=n-1; i>=0; i--) cout<<u[i];
	cout<<endl;
	for (int i=n-1; i>=0; i--) cout<<v[i];
	cout<<endl;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <queue>

#define LL long long
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define size(S) S.size()

using namespace std;

int n, m, a[10], b[10], o[10], ans, la, lb;
int x[100010], y[100010], u[100010], v[100010];
string s;

int main(){
	cin>>s;
	n=s.length();
	for (int i=0; i<n; i++) o[s[i]-'0']++;
	ans=-1;
	for (int i=1; i<=5; i++){
		memcpy(a, o, sizeof(a));
		memcpy(b, o, sizeof(b));
		a[i]--; b[10-i]--;
		x[0]=i; y[0]=10-i;
		int tmp=0;
		if (a[i]<0 || b[10-i]<0){
			a[i]++; b[10-i]++;
			int kk=min(a[0], b[0]);
			if (tmp+min(a[0], b[0])>ans){
				int p=tmp;
				a[0]-=min(a[0], b[0]);
				b[0]-=min(a[0], b[0]);
				for (int k=0; k<10; k++)
					while (a[k]) a[k]--, x[p++]=k;
				la=p;
				p=tmp;
				for (int k=1; k<10; k++)
					while (b[k]) b[k]--, y[p++]=k;
				lb=p;
				ans=tmp+kk;
				memcpy(u, x, sizeof(u));
				memcpy(v, y, sizeof(v));
			}
			continue;
		}

		tmp=1;
		for (int k=0; k<10; k++){
			for (int w=1; a[k]>0 && b[9-k]>0; w++){
				a[k]--; b[9-k]--;
				x[tmp]=k; y[tmp]=9-k;
				tmp++;
			}
		}
		if (tmp+min(a[0], b[0])>ans){
			int kk=min(a[0], b[0]);
			int p=tmp;
			a[0]-=min(a[0], b[0]);
			b[0]-=min(a[0], b[0]);
			for (int k=0; k<10; k++)
				while (a[k]) a[k]--, x[p++]=k;
			la=p;
			p=tmp;
			for (int k=1; k<10; k++)
				while (b[k]) b[k]--, y[p++]=k;
			lb=p;
			ans=tmp+kk;
			memcpy(u, x, sizeof(u));
			memcpy(v, y, sizeof(v));
		}
	}
	for (int i=la-1; i>=0; i--) cout<<u[i];
	for (int i=0; i<n-la; i++) cout<<0;
	cout<<endl;
	for (int i=lb-1; i>=0; i--) cout<<v[i];
	for (int i=0; i<n-lb; i++) cout<<0;
	cout<<endl;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <queue>

#define LL long long
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define size(S) S.size()

using namespace std;

int n, m, a[10], b[10], o[10], ans;
int x[100010], y[100010], u[100010], v[100010];
string s;

int main(){
	cin>>s;
	n=s.length();
	for (int i=0; i<(int)s.length(); i++) o[s[i]-'0']++;
	for (int i=0; i<=5; i++){
		memcpy(a, o, sizeof(a));
		memcpy(b, o, sizeof(b));
		if (i==0){
			a[0]--; b[0]--;
			x[0]=y[0]=0;
			if (a[0]<0 || b[0]<0) continue;
		} else{
			a[i]--; b[10-i]--;
			x[0]=i; y[0]=10-i;
			if (a[i]<0 || b[10-i]<0) continue;
		}
		int tmp=1;
		for (int k=0; k<10; k++){
			for (int w=1; a[k]>0 && b[9-k]>0; w++){
				a[k]--; b[9-k]--;
				x[tmp]=k; y[tmp]=9-k;
				tmp++;
			}
		}
		if (tmp>ans){
			int p=tmp;
			for (int k=0; k<10; k++)
				while (a[k]) a[k]--, x[p++]=k;
			p=tmp;
			for (int k=0; k<10; k++)
				while (b[k]) b[k]--, y[p++]=k;
			ans=tmp;
			memcpy(u, x, sizeof(u));
			memcpy(v, y, sizeof(v));
		}
	}
	cout<<ans<<endl;
	for (int i=n-1; i>=0; i--) cout<<u[i];
	cout<<endl;
	for (int i=n-1; i>=0; i--) cout<<v[i];
	cout<<endl;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <queue>

#define LL long long
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define size(S) S.size()

using namespace std;

int n, m, a[10], b[10], o[10], ans, la, lb;
int x[100010], y[100010], u[100010], v[100010];
string s;

int main(){
	cin>>s;
	n=s.length();
	for (int i=0; i<n; i++) o[s[i]-'0']++;
	ans=-1;
	for (int i=1; i<=5; i++){
		memcpy(a, o, sizeof(a));
		memcpy(b, o, sizeof(b));
		a[i]--; b[10-i]--;
		x[0]=i; y[0]=10-i;
		int tmp=0;
		if (a[i]<0 || b[10-i]<0){
			a[i]++; b[10-i]++;
			if (tmp+min(a[0], b[0])>ans){
				int p=tmp;
				a[0]-=min(a[0], b[0]);
				b[0]-=min(a[0], b[0]);
				for (int k=0; k<10; k++)
					while (a[k]) a[k]--, x[p++]=k;
				la=p;
				p=tmp;
				for (int k=1; k<10; k++)
					while (b[k]) b[k]--, y[p++]=k;
				lb=p;
				ans=tmp+min(a[0], b[0]);
				memcpy(u, x, sizeof(u));
				memcpy(v, y, sizeof(v));
			}
			continue;
		}

		tmp=1;
		for (int k=0; k<10; k++){
			for (int w=1; a[k]>0 && b[9-k]>0; w++){
				a[k]--; b[9-k]--;
				x[tmp]=k; y[tmp]=9-k;
				tmp++;
			}
		}
		if (tmp+min(a[0], b[0])>ans){
			int p=tmp;
			a[0]-=min(a[0], b[0]);
			b[0]-=min(a[0], b[0]);
			for (int k=0; k<10; k++)
				while (a[k]) a[k]--, x[p++]=k;
			la=p;
			p=tmp;
			for (int k=1; k<10; k++)
				while (b[k]) b[k]--, y[p++]=k;
			lb=p;
			ans=tmp+min(a[0], b[0]);
			memcpy(u, x, sizeof(u));
			memcpy(v, y, sizeof(v));
		}
	}
	for (int i=la-1; i>=0; i--) cout<<u[i];
	for (int i=0; i<n-la; i++) cout<<0;
	cout<<endl;
	for (int i=lb-1; i>=0; i--) cout<<v[i];
	for (int i=0; i<n-lb; i++) cout<<0;
	cout<<endl;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>

using namespace std;

typedef long long ll;

const int N = 1000010, D = 20;
int n, c1[ D ], c2[ D ], cnt[ D ], mC = -1, bnf, df1, df2;
int s1[ N ], s2[ N ];
char s[ N ];

int sup( int i ) { return ( 10 - i ) % 10; }

void stavi( )
{
    int i, bn = bnf;
    for ( i = n; bnf; i--, bnf-- ) s1[ i ] = s2[ i ] = 0;
    s1[ i ] = df1; s2[ i ] = df2;
    i--;
    for ( int j = 0; j <= 9; j++ ) c1[ j ] = c2[ j ] = cnt[ j ];
    c1[ 0 ] -= bn; c2[ 0 ] -= bn;
    c1[ df1 ]--; c2[ df2 ]--;
    for ( ; i; i-- )
    {
        int j;
        //printf( "nova\n" );
        //for ( j = 0; j <= 9; j++ ) printf( "%d %d\n", c1[ j ], c2[ j ] );
        for ( j = 0; j <= 9; j++ ) if ( c1[ j ] && c2[ 9 - j ] )
        {
            s1[ i ] = j; s2[ i ] = 9 - j;
            c1[ j ]--; c2[ 9 - j ]--;
            break;
        }
        if ( j < 10 ) continue;
        for ( j = 0; j <= 9; j++ )
        {
            bool f = false;
            for ( int k = 0; k <= 9; k++ ) if ( c1[ j ] && c2[ k ] )
            {
                s1[ i ] = j; s2[ i ] = k;
                c1[ j ]--; c2[ k ]--;
                f = true;
                break;
            }
            if ( f ) break;
        }
    }
}

int main( )
{
    scanf( "%s", s + 1 );
    n = strlen( s + 1 );
    for ( int i = 1; i <= n; i++ ) cnt[ s[ i ] - '0' ]++;
    //printf( "sad\n" );
    //for ( int i = 0; i <= 9; i++ ) printf( "%d %d\n", cnt[ i ], cnt[ i ] );
    for ( int i = 1; i <= n; i++ ) s1[ i ] = s2[ i ] = s[ i ] - '0';
    for ( int bn = 0; bn <= cnt[ 0 ]; bn++ )
    {
        for ( int i = 0; i <= 9; i++ ) c1[ i ] = c2[ i ] = cnt[ i ];
        c1[ 0 ] -= bn; c2[ 0 ] -= bn;
        if ( bn > mC )
        {
            for ( int g = 1; g <= 9; g++ )
            {
                bool f = false;
                for ( int h = 1; h <= 9; h++ ) if ( c1[ g ] && c2[ h ] )
                {
                    bnf = bn;
                    df1 = g;
                    df2 = h;
                    mC = bn;
                }
                if ( f ) break;
            }
        }
        /*if ( bn == 2 )
        {
            for ( int i = 0; i <= 9; i++ ) printf( "%d %d\n", c1[ i ], c2[ i ] );
            printf( "\n" );
        }*/
        for ( int i = 1; i <= 9; i++ ) if ( c1[ i ] && c2[ sup( i ) ] )
        {
            c1[ i ]--; c2[ sup( i ) ]--;
            int C = bn + 1;
            /*if ( bn == 2 && i == 3 )
            {
                printf( "sad\n" );
                for ( int i = 0; i <= 9; i++ ) printf( "%d %d\n", c1[ i ], c2[ i ] );
                printf( "gotovo %d\n", C );
            }*/
            for ( int j = 0; j <= 9; j++ ) C += min( c1[ j ], c2[ 9 - j ] );
            if ( C > mC )
            {
                mC = C;
                bnf = bn;
                df1 = i;
                df2 = sup( i );
            }
            c1[ i ]++; c2[ sup( i ) ]++;
        }
    }
    if ( mC != -1 ) stavi( );
    //printf( "%d\n", mC );
    for ( int i = 1; i <= n; i++ ) printf( "%c", s1[ i ] + '0' );
    printf( "\n" );
    for ( int i = 1; i <= n; i++ ) printf( "%c", s2[ i ] + '0' );
    printf( "\n" );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>

using namespace std;

typedef long long ll;

const int N = 1000010, D = 20;
int n, c1[ D ], c2[ D ], cnt[ D ], mC = -1, bnf, df;
int s1[ N ], s2[ N ];
char s[ N ];

int sup( int i ) { return ( 10 - i ) % 10; }

void stavi( )
{
    int i, bn = bnf;
    for ( i = n; bnf; i--, bnf-- ) s1[ i ] = s2[ i ] = 0;
    s1[ i ] = df; s2[ i ] = sup( df );
    i--;
    for ( int j = 0; j <= 9; j++ ) c1[ j ] = c2[ j ] = cnt[ j ];
    c1[ 0 ] -= bn; c2[ 0 ] -= bn;
    c1[ df ]--; c2[ sup( df ) ]--;
    for ( ; i; i-- )
    {
        int j;
        //printf( "nova\n" );
        //for ( j = 0; j <= 9; j++ ) printf( "%d %d\n", c1[ j ], c2[ j ] );
        for ( j = 0; j <= 9; j++ ) if ( c1[ j ] && c2[ 9 - j ] )
        {
            s1[ i ] = j; s2[ i ] = 9 - j;
            c1[ j ]--; c2[ 9 - j ]--;
            break;
        }
        if ( j < 10 ) continue;
        for ( j = 0; j <= 9; j++ )
        {
            bool f = false;
            for ( int k = 0; k <= 9; k++ ) if ( c1[ j ] && c2[ k ] )
            {
                s1[ i ] = j; s2[ i ] = k;
                c1[ j ]--; c2[ k ]--;
                f = true;
                break;
            }
            if ( f ) break;
        }
    }
}

int main( )
{
    scanf( "%s", s + 1 );
    n = strlen( s + 1 );
    for ( int i = 1; i <= n; i++ ) cnt[ s[ i ] - '0' ]++;
    //printf( "sad\n" );
    //for ( int i = 0; i <= 9; i++ ) printf( "%d %d\n", cnt[ i ], cnt[ i ] );
    for ( int i = 1; i <= n; i++ ) s1[ i ] = s2[ i ] = s[ i ] - '0';
    for ( int bn = 0; bn <= cnt[ 0 ]; bn++ )
    {
        for ( int i = 0; i <= 9; i++ ) c1[ i ] = c2[ i ] = cnt[ i ];
        c1[ 0 ] -= bn; c2[ 0 ] -= bn;
        for ( int i = 1; i <= 9; i++ ) if ( c1[ i ] && c2[ sup( i ) ] )
        {
            c1[ i ]--; c2[ sup( i ) ]--;
            int C = bn + 1;
            for ( int j = 0; j <= 9; j++ ) C += min( c1[ j ], c2[ 9 - j ] );
            if ( C > mC )
            {
                mC = C;
                bnf = bn;
                df = i;
            }
        }
    }
    if ( mC != -1 ) stavi( );
    for ( int i = 1; i <= n; i++ ) printf( "%c", s1[ i ] + '0' );
    printf( "\n" );
    for ( int i = 1; i <= n; i++ ) printf( "%c", s2[ i ] + '0' );
    printf( "\n" );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>

using namespace std;

typedef long long ll;

const int N = 1000010, D = 20;
int n, c1[ D ], c2[ D ], cnt[ D ], mC = -1, bnf, df1, df2;
int s1[ N ], s2[ N ];
char s[ N ];

int sup( int i ) { return ( 10 - i ) % 10; }

void stavi( )
{
    int i, bn = bnf;
    for ( i = n; bnf; i--, bnf-- ) s1[ i ] = s2[ i ] = 0;
    s1[ i ] = df1; s2[ i ] = df2;
    i--;
    for ( int j = 0; j <= 9; j++ ) c1[ j ] = c2[ j ] = cnt[ j ];
    c1[ 0 ] -= bn; c2[ 0 ] -= bn;
    c1[ df1 ]--; c2[ df2 ]--;
    for ( ; i; i-- )
    {
        int j;
        //printf( "nova\n" );
        //for ( j = 0; j <= 9; j++ ) printf( "%d %d\n", c1[ j ], c2[ j ] );
        for ( j = 0; j <= 9; j++ ) if ( c1[ j ] && c2[ 9 - j ] )
        {
            s1[ i ] = j; s2[ i ] = 9 - j;
            c1[ j ]--; c2[ 9 - j ]--;
            break;
        }
        if ( j < 10 ) continue;
        for ( j = 0; j <= 9; j++ )
        {
            bool f = false;
            for ( int k = 0; k <= 9; k++ ) if ( c1[ j ] && c2[ k ] )
            {
                s1[ i ] = j; s2[ i ] = k;
                c1[ j ]--; c2[ k ]--;
                f = true;
                break;
            }
            if ( f ) break;
        }
    }
}

int main( )
{
    scanf( "%s", s + 1 );
    n = strlen( s + 1 );
    for ( int i = 1; i <= n; i++ ) cnt[ s[ i ] - '0' ]++;
    //printf( "sad\n" );
    //for ( int i = 0; i <= 9; i++ ) printf( "%d %d\n", cnt[ i ], cnt[ i ] );
    for ( int i = 1; i <= n; i++ ) s1[ i ] = s2[ i ] = s[ i ] - '0';
    for ( int bn = 0; bn <= cnt[ 0 ]; bn++ )
    {
        for ( int i = 0; i <= 9; i++ ) c1[ i ] = c2[ i ] = cnt[ i ];
        c1[ 0 ] -= bn; c2[ 0 ] -= bn;
        if ( bn > mC )
        {
            for ( int g = 1; g <= 9; g++ )
            {
                bool f = false;
                for ( int h = 1; h <= 9; h++ ) if ( c1[ g ] && c2[ h ] )
                {
                    bnf = bn;
                    df1 = g;
                    df2 = h;
                    mC = bn;
                }
                if ( f ) break;
            }
        }
        for ( int i = 1; i <= 9; i++ ) if ( c1[ i ] && c2[ sup( i ) ] )
        {
            c1[ i ]--; c2[ sup( i ) ]--;
            int C = bn + 1;
            for ( int j = 0; j <= 9; j++ ) C += min( c1[ j ], c2[ 9 - j ] );
            if ( C > mC )
            {
                mC = C;
                bnf = bn;
                df1 = i;
                df2 = sup( i );
            }
        }
    }
    if ( mC != -1 ) stavi( );
    for ( int i = 1; i <= n; i++ ) printf( "%c", s1[ i ] + '0' );
    printf( "\n" );
    for ( int i = 1; i <= n; i++ ) printf( "%c", s2[ i ] + '0' );
    printf( "\n" );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>

using namespace std;

typedef long long ll;

const int N = 1000010, D = 20;
int n, c1[ D ], c2[ D ], cnt[ D ], mC = -1;
int s1[ N ], s2[ N ];
char s[ N ];

int sup( int i ) { return ( 10 - i ) % 10; }

void stavi( int g, int h )
{
    s1[ n ] = g; s2[ n ] = h;
    for ( int i = n - 1; i; i-- )
    {
        int j;
        for ( j = 0; j <= 9; j++ ) if ( c1[ j ] && c2[ 9 - j ] )
        {
            s1[ i ] = j; s2[ i ] = 9 - j;
            c1[ j ]--; c2[ 9 - j ]--;
            break;
        }
        if ( j < 10 ) continue;
        for ( j = 0; j <= 9; j++ )
        {
            bool f = false;
            for ( int k = 0; k <= 9; k++ ) if ( c1[ j ] && c2[ k ] )
            {
                s1[ i ] = j; s2[ i ] = k;
                c1[ j ]--; c2[ k ]--;
                f = true;
                break;
            }
            if ( f ) break;
        }
    }
}

int main( )
{
    scanf( "%s", s + 1 );
    n = strlen( s + 1 );
    for ( int i = 1; i <= n; i++ ) cnt[ s[ i ] - '0' ]++;
    for ( int i = 1; i <= n; i++ ) s1[ i ] = s2[ i ] = s[ i ] - '0';
    for ( int i = 0; i <= 9; i++ )
    {
        int d = sup( i );
        for ( int j = 0; j <= 9; j++ ) c1[ j ] = c2[ j ] = cnt[ j ];
        if ( !c1[ i ] || !c2[ d ] ) continue;
        c1[ i ]--; c2[ d ]--;
        int C = 1;
        for ( int j = 0; j <= 9; j++ ) C += min( c1[ j ], c2[ 9 - j ] );
        if ( C > mC )
        {
            mC = C;
            stavi( i, d );
        }
    }
    for ( int i = 1; i <= n; i++ ) printf( "%c", s1[ i ] + '0');
    printf( "\n" );
    for ( int i = 1; i <= n; i++ ) printf( "%c", s2[ i ] + '0' );
    printf( "\n" );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>

using namespace std;

typedef long long ll;

const int N = 1000010, D = 20;
int n, c1[ D ], c2[ D ], cnt[ D ], mC = -1, bnf, df1, df2;
int s1[ N ], s2[ N ];
char s[ N ];

int sup( int i ) { return ( 10 - i ) % 10; }

void stavi( )
{
    int i, bn = bnf;
    for ( i = n; bnf; i--, bnf-- ) s1[ i ] = s2[ i ] = 0;
    s1[ i ] = df1; s2[ i ] = df2;
    i--;
    for ( int j = 0; j <= 9; j++ ) c1[ j ] = c2[ j ] = cnt[ j ];
    c1[ 0 ] -= bn; c2[ 0 ] -= bn;
    c1[ df1 ]--; c2[ df2 ]--;
    for ( ; i; i-- )
    {
        int j;
        //printf( "nova\n" );
        //for ( j = 0; j <= 9; j++ ) printf( "%d %d\n", c1[ j ], c2[ j ] );
        for ( j = 0; j <= 9; j++ ) if ( c1[ j ] && c2[ 9 - j ] )
        {
            s1[ i ] = j; s2[ i ] = 9 - j;
            c1[ j ]--; c2[ 9 - j ]--;
            break;
        }
        if ( j < 10 ) continue;
        for ( j = 0; j <= 9; j++ )
        {
            bool f = false;
            for ( int k = 0; k <= 9; k++ ) if ( c1[ j ] && c2[ k ] )
            {
                s1[ i ] = j; s2[ i ] = k;
                c1[ j ]--; c2[ k ]--;
                f = true;
                break;
            }
            if ( f ) break;
        }
    }
}

int main( )
{
    scanf( "%s", s + 1 );
    n = strlen( s + 1 );
    for ( int i = 1; i <= n; i++ ) cnt[ s[ i ] - '0' ]++;
    //printf( "sad\n" );
    //for ( int i = 0; i <= 9; i++ ) printf( "%d %d\n", cnt[ i ], cnt[ i ] );
    for ( int i = 1; i <= n; i++ ) s1[ i ] = s2[ i ] = s[ i ] - '0';
    for ( int bn = 0; bn <= cnt[ 0 ]; bn++ )
    {
        for ( int i = 0; i <= 9; i++ ) c1[ i ] = c2[ i ] = cnt[ i ];
        c1[ 0 ] -= bn; c2[ 0 ] -= bn;
        if ( bn > mC )
        {
            for ( int g = 1; g <= 9; g++ )
            {
                bool f = false;
                for ( int h = 1; h <= 9; h++ ) if ( c1[ g ] && c2[ h ] )
                {
                    bnf = bn;
                    df1 = g;
                    df2 = h;
                    mC = bn;
                }
                if ( f ) break;
            }
        }
        /*if ( bn == 2 )
        {
            for ( int i = 0; i <= 9; i++ ) printf( "%d %d\n", c1[ i ], c2[ i ] );
            printf( "\n" );
        }*/
        for ( int i = 1; i <= 9; i++ ) if ( c1[ i ] && c2[ sup( i ) ] )
        {
            c1[ i ]--; c2[ sup( i ) ]--;
            int C = bn + 1;
            /*if ( bn == 2 && i == 3 )
            {
                printf( "sad\n" );
                for ( int i = 0; i <= 9; i++ ) printf( "%d %d\n", c1[ i ], c2[ i ] );
                printf( "gotovo %d\n", C );
            }*/
            for ( int j = 0; j <= 9; j++ ) C += min( c1[ j ], c2[ 9 - j ] );
            if ( C > mC )
            {
                mC = C;
                bnf = bn;
                df1 = i;
                df2 = sup( i );
            }
            for ( int i = 0; i <= 9; i++ ) c1[ i ] = c2[ i ] = cnt[ i ];
        }
    }
    if ( mC != -1 ) stavi( );
    //printf( "%d\n", mC );
    for ( int i = 1; i <= n; i++ ) printf( "%c", s1[ i ] + '0' );
    printf( "\n" );
    for ( int i = 1; i <= n; i++ ) printf( "%c", s2[ i ] + '0' );
    printf( "\n" );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define N 200000
char a[ N ], b[ N ];
int ia[ 10 ], ib[ 10 ];
char linha[ N ];

int main(){
	scanf( "%s", linha );
	memset( ia, 0, sizeof ia );
	memset( ib, 0, sizeof ib );

	for( int i = 0; linha[i]; ++i ){
		++ia[ linha[i] - '0' ];
		++ib[ linha[i] - '0' ];
	}

	int maior = 0, index = 1;
	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = 1;
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 0; j <= 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = 1 + ia[ 0 ];
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 1; j < 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = -i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	if( !maior ){
		int prt = strlen( linha ) - 1;
		for( int i = 0; i < 10; ++i ){
			while( ia[ i ] ){
				linha[ prt-- ] = i + '0';
				--ia[ i ];
			}
		}

		printf( "%s\n%s\n", linha, linha );
	}else{
		int prt = 0;
		if( index < 0 ){
			index = -index;
			while( ia[ 0 ] ){
				a[ prt ] = '0';
				b[ prt++ ] = '0';
				--ia[ 0 ];
				--ib[ 0 ];
			}
		}
		a[ prt ] = index + '0';
		b[ prt ] = 10 - index + '0';
		--ia[ index ];
		--ib[ 10 - index ];
		++prt;

		for( int i = 0; i < 10; ++i ){
			int qtd = min( ia[ i ], ib[ 9 - i ] );
			if( qtd > 0 ){
				for( int j = 0; j < qtd; ++j ){
					a[ prt ] = i + '0';
					b[ prt++ ] = 9 - i + '0';
				}
				ia[ i ] -= qtd;
				ib[ 9 - i ] -= qtd;
			}
		}

		int pa = prt, pb = prt;
		for( int i = 1; i < 10; ++i ){
			while( ia[ i ] ){
				a[ pa++ ] = i + '0';
				--ia[ i ];
			}

			while( ib[ i ] ){
				b[ pb++ ] = i + '0';
				--ib[ i ];
			}
		}

		while( ia[ 0 ] > ib[ 0 ] ){
			a[ pa++ ] = '0';
			--ia[ 0 ];
		}

		while( ib[ 0 ] > ia[ 0 ] ){
			b[ pb++ ] = '0';
			--ib[ 0 ];
		}
		reverse( a, a + pa );
		reverse( b, b + pb );

		while( ia[ 0 ] && ib[ 0 ] ){
			a[ pa++ ] = b[ pb++ ] = '0';
			--ia[ 0 ];
			--ib[ 0 ];
		}
		a[ pa ] = 0;
		b[ pb ] = 0;
		printf( "%s\n%s\n", a, b );
	}

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define N 200000
char a[ N ], b[ N ];
int ia[ 10 ], ib[ 10 ];
char linha[ N ];

int main(){
    scanf( "%s", linha );
    memset( ia, 0, sizeof ia );
    memset( ib, 0, sizeof ib );

    for( int i = 0; linha[i]; ++i ){
        ++ia[ linha[i] - '0' ];
        ++ib[ linha[i] - '0' ];
    }

    int maior = 0, index = 1;
    for( int i = 1; i < 10; ++i ){
        if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
            int tmp = 1;
            --ia[ i ];
            --ib[ 10 - i ];

            int resto = 0;
            for( int j = 0; j <= 9; ++j ){
                int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
                if( qtd > 0 ) tmp += qtd;

                if( !j ){
                    if( ia[ j ] > ib[ 9 - j ] ) resto += ia[ j ] - ib[ 9 - j ];
                }else if( j == 9 ){
                    if( ia[ j ] < ib[ 9 - j ] ) resto = min( resto, ib[ 9 - j ] - ia[ j ] );
                }

            }
            tmp += resto;

            if( tmp > maior ){
                maior = tmp;
                index = i;
            }
            ++ia[ i ];
            ++ib[ 10 - i ];
        }
    }

    for( int i = 1; i < 10; ++i ){
        if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
            int tmp = 1 + ia[ 0 ];
            --ia[ i ];
            --ib[ 10 - i ];

            for( int j = 1; j < 9; ++j ){
                int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
                if( qtd > 0 ) tmp += qtd;
            }
            if( tmp > maior ){
                maior = tmp;
                index = -i;
            }
            ++ia[ i ];
            ++ib[ 10 - i ];
        }
    }

    if( !maior ){
        int prt = strlen( linha ) - 1;
        for( int i = 0; i < 10; ++i ){
            while( ia[ i ] ){
                linha[ prt-- ] = i + '0';
                --ia[ i ];
            }
        }

        printf( "%s\n%s\n", linha, linha );
    }else{
        int prt = 0;
        if( index < 0 ){
            index = -index;
            while( ia[ 0 ] ){
                a[ prt ] = '0';
                b[ prt++ ] = '0';
                --ia[ 0 ];
                --ib[ 0 ];
            }
        }
        a[ prt ] = index + '0';
        b[ prt ] = 10 - index + '0';
        --ia[ index ];
        --ib[ 10 - index ];
        ++prt;

        for( int i = 0; i < 10; ++i ){
            int qtd = min( ia[ i ], ib[ 9 - i ] );
            if( qtd > 0 ){
                for( int j = 0; j < qtd; ++j ){
                    a[ prt ] = i + '0';
                    b[ prt++ ] = 9 - i + '0';
                }
                ia[ i ] -= qtd;
                ib[ 9 - i ] -= qtd;
            }
        }

        int pa = prt, pb = prt;
        for( int i = 1; i < 10; ++i ){
            while( ia[ i ] ){
                a[ pa++ ] = i + '0';
                --ia[ i ];
            }

            while( ib[ i ] ){
                b[ pb++ ] = i + '0';
                --ib[ i ];
            }
        }

        while( ia[ 0 ] > ib[ 0 ] ){
            a[ pa++ ] = '0';
            --ia[ 0 ];
        }

        while( ib[ 0 ] > ia[ 0 ] ){
            b[ pb++ ] = '0';
            --ib[ 0 ];
        }
        reverse( a, a + pa );
        reverse( b, b + pb );

        while( ia[ 0 ] && ib[ 0 ] ){
            a[ pa++ ] = b[ pb++ ] = '0';
            --ia[ 0 ];
            --ib[ 0 ];
        }
        a[ pa ] = 0;
        b[ pb ] = 0;
        printf( "%s\n%s\n", a, b );
    }

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define N 200000
char a[ N ], b[ N ];
int ia[ 10 ], ib[ 10 ];
char linha[ N ];

int main(){
	scanf( "%s", linha );
	memset( ia, 0, sizeof ia );
	memset( ib, 0, sizeof ib );

	for( int i = 0; linha[i]; ++i ){
		++ia[ linha[i] - '0' ];
		++ib[ linha[i] - '0' ];
	}

	int maior = 0, index = 1;
	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = 1;
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 0; j < 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	if( !maior ){
		int prt = strlen( linha ) - 1;
		for( int i = 0; i < 10; ++i ){
			while( ia[ i ] ){
				linha[ prt-- ] = i + '0';
				--ia[ i ];
			}
		}

		printf( "%s\n%s\n", linha, linha );
	}else{
		int prt = 0;

		a[ prt ] = index + '0';
		b[ prt ] = 10 - index + '0';
		--ia[ index ];
		--ib[ 10 - index ];
		++prt;

		for( int i = 1; i < 10; ++i ){
			int qtd = min( ia[ i ], ib[ 9 - i ] );
			if( qtd > 0 ){
				for( int j = 0; j < qtd; ++j ){
					a[ prt ] = i + '0';
					b[ prt++ ] = 9 - i + '0';
				}
				ia[ i ] -= qtd;
				ib[ 9 - i ] -= qtd;
			}
		}

		int pa = prt, pb = prt;
		for( int i = 1; i < 10; ++i ){
			while( ia[ i ] ){
				a[ pa++ ] = i + '0';
				--ia[ i ];
			}

			while( ib[ i ] ){
				b[ pb++ ] = i + '0';
				--ib[ i ];
			}
		}

		while( ia[ 0 ] > ib[ 0 ] ){
			a[ pa++ ] = '0';
			--ia[ 0 ];
		}

		while( ib[ 0 ] > ia[ 0 ] ){
			b[ pb++ ] = '0';
			--ib[ 0 ];
		}
		reverse( a, a + pa );
		reverse( b, b + pb );

		while( ia[ 0 ] && ib[ 0 ] ){
			a[ pa++ ] = b[ pb++ ] = '0';
			--ia[ 0 ];
			--ib[ 0 ];
		}
		a[ pa ] = 0;
		b[ pb ] = 0;
		printf( "%s\n%s\n", a, b );
	}

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define N 200000
char a[ N ], b[ N ];
int ia[ 10 ], ib[ 10 ];
char linha[ N ];

int main(){
	scanf( "%s", linha );
	memset( ia, 0, sizeof ia );
	memset( ib, 0, sizeof ib );

	for( int i = 0; linha[i]; ++i ){
		++ia[ linha[i] - '0' ];
		++ib[ linha[i] - '0' ];
	}

	int maior = 0, index = 1;
	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = ia[ 0 ] + 1;
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 1; j < 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	if( !maior ){
		int prt = strlen( linha ) - 1;
		for( int i = 0; i < 10; ++i ){
			while( ia[ i ] ){
				linha[ prt-- ] = i + '0';
				--ia[ i ];
			}
		}

		printf( "%s\n%s\n", linha, linha );
	}else{
		int prt = 0;
		for( int i = 0; i < ia[0]; ++i ){
			a[ prt ] = b[ prt ] = '0';
			++prt;
		}
		ia[ 0 ] = ib[ 0 ] = 0;

		a[ prt ] = index + '0';
		b[ prt ] = 10 - index + '0';
		--ia[ index ];
		--ib[ 10 - index ];
		++prt;

		for( int i = 1; i < 10; ++i ){
			int qtd = min( ia[ i ], ib[ 9 - i ] );
			if( qtd > 0 ){
				for( int j = 0; j < qtd; ++j ){
					a[ prt ] = i + '0';
					b[ prt++ ] = 9 - i + '0';
				}
				ia[ i ] -= qtd;
				ib[ 9 - i ] -= qtd;
			}
		}

		int pa = prt, pb = prt;
		for( int i = 1; i < 10; ++i ){
			while( ia[ i ] ){
				a[ pa++ ] = i + '0';
				--ia[ i ];
			}

			while( ib[ i ] ){
				b[ pb++ ] = i + '0';
				--ib[ i ];
			}
		}
		int tam = strlen( linha );
		reverse( a, a + tam );
		reverse( b, b + tam );
		a[ tam ] = 0;
		b[ tam ] = 0;
		printf( "%s\n%s\n", a, b );
	}

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define N 200000
char a[ N ], b[ N ];
int ia[ 10 ], ib[ 10 ];
char linha[ N ];

int main(){
    scanf( "%s", linha );
    memset( ia, 0, sizeof ia );
    memset( ib, 0, sizeof ib );

    for( int i = 0; linha[i]; ++i ){
        ++ia[ linha[i] - '0' ];
        ++ib[ linha[i] - '0' ];
    }

    int maior = 0, index = 1;
    for( int i = 1; i < 10; ++i ){
        if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
            int tmp = 1;
            --ia[ i ];
            --ib[ 10 - i ];

            for( int j = 0; j <= 9; ++j ){
                int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
                if( qtd > 0 ) tmp += qtd;
            }
            if( tmp > maior ){
                maior = tmp;
                index = i;
            }
            ++ia[ i ];
            ++ib[ 10 - i ];
        }
    }

    for( int i = 1; i < 10; ++i ){
        if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
            int tmp = 1 + ia[ 0 ];
            --ia[ i ];
            --ib[ 10 - i ];

            for( int j = 1; j < 9; ++j ){
                int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
                if( qtd > 0 ) tmp += qtd;
            }
            if( tmp > maior ){
                maior = tmp;
                index = -i;
            }
            ++ia[ i ];
            ++ib[ 10 - i ];
        }
    }

    if( !maior ){
        int prt = strlen( linha ) - 1;
        for( int i = 0; i < 10; ++i ){
            while( ia[ i ] ){
                linha[ prt-- ] = i + '0';
                --ia[ i ];
            }
        }

        printf( "%s\n%s\n", linha, linha );
    }else{
        int prt = 0;
        if( index < 0 ){
            index = -index;
            while( ia[ 0 ] ){
                a[ prt ] = '0';
                b[ prt++ ] = '0';
                --ia[ 0 ];
                --ib[ 0 ];
            }
        }
        a[ prt ] = index + '0';
        b[ prt ] = 10 - index + '0';
        --ia[ index ];
        --ib[ 10 - index ];
        ++prt;

        for( int i = 0; i < 10; ++i ){
            int qtd = min( ia[ i ], ib[ 9 - i ] );
            if( qtd > 0 ){
                for( int j = 0; j < qtd; ++j ){
                    a[ prt ] = i + '0';
                    b[ prt++ ] = 9 - i + '0';
                }
                ia[ i ] -= qtd;
                ib[ 9 - i ] -= qtd;
            }
        }

        int pa = prt, pb = prt;
        for( int i = 1; i < 10; ++i ){
            while( ia[ i ] ){
                a[ pa++ ] = i + '0';
                --ia[ i ];
            }

            while( ib[ i ] ){
                b[ pb++ ] = i + '0';
                --ib[ i ];
            }
        }

        while( ia[ 0 ] > ib[ 0 ] ){
            a[ pa++ ] = '0';
            --ia[ 0 ];
        }

        while( ib[ 0 ] > ia[ 0 ] ){
            b[ pb++ ] = '0';
            --ib[ 0 ];
        }
        reverse( a, a + pa );
        reverse( b, b + pb );

        while( ia[ 0 ] && ib[ 0 ] ){
            a[ pa++ ] = b[ pb++ ] = '0';
            --ia[ 0 ];
            --ib[ 0 ];
        }
        a[ pa ] = 0;
        b[ pb ] = 0;
        printf( "%s\n%s\n", a, b );
    }

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define N 200000
char a[ N ], b[ N ];
int ia[ 10 ], ib[ 10 ];
char linha[ N ];

int main(){
	scanf( "%s", linha );
	memset( ia, 0, sizeof ia );
	memset( ib, 0, sizeof ib );

	for( int i = 0; linha[i]; ++i ){
		++ia[ linha[i] - '0' ];
		++ib[ linha[i] - '0' ];
	}

	int maior = 0, index = 1;
	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = ia[ 0 ] + 1;
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 1; j < 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	if( !maior ) printf( "%s\n%s\n", linha, linha );
	else{
		int prt = 0;
		for( int i = 0; i < ia[0]; ++i ){
			a[ prt ] = b[ prt ] = '0';
			++prt;
		}
		ia[ 0 ] = ib[ 0 ] = 0;

		a[ prt ] = index + '0';
		b[ prt ] = 10 - index + '0';
		--ia[ index ];
		--ib[ 10 - index ];
		++prt;

		for( int i = 1; i < 10; ++i ){
			int qtd = min( ia[ i ], ib[ 9 - i ] );
			if( qtd > 0 ){
				for( int j = 0; j < qtd; ++j ){
					a[ prt ] = i + '0';
					b[ prt++ ] = 9 - i + '0';
				}
				ia[ i ] -= qtd;
				ib[ 9 - i ] -= qtd;
			}
		}

		int pa = prt, pb = prt;
		for( int i = 1; i < 10; ++i ){
			while( ia[ i ] ){
				a[ pa++ ] = i + '0';
				--ia[ i ];
			}

			while( ib[ i ] ){
				b[ pb++ ] = i + '0';
				--ib[ i ];
			}
		}
		int tam = strlen( linha );
		reverse( a, a + tam );
		reverse( b, b + tam );
		a[ tam ] = 0;
		b[ tam ] = 0;
		printf( "%s\n%s\n", a, b );
	}

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define N 200000
char a[ N ], b[ N ];
int ia[ 10 ], ib[ 10 ];
char linha[ N ];

int main(){
	scanf( "%s", linha );
	memset( ia, 0, sizeof ia );
	memset( ib, 0, sizeof ib );

	for( int i = 0; linha[i]; ++i ){
		++ia[ linha[i] - '0' ];
		++ib[ linha[i] - '0' ];
	}

	int maior = 0, index = 1;
	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = 1;
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 0; j <= 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = 1 + ia[ 0 ];
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 1; j < 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = -i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	if( !maior ){
		int prt = strlen( linha ) - 1;
		for( int i = 0; i < 10; ++i ){
			while( ia[ i ] ){
				linha[ prt-- ] = i + '0';
				--ia[ i ];
			}
		}

		printf( "%s\n%s\n", linha, linha );
	}else{
		int prt = 0;
		if( index < 0 ){
			index = -index;
			while( ia[ 0 ] ){
				a[ prt ] = '0';
				b[ prt++ ] = '0';
				--ia[ 0 ];
				--ib[ 0 ];
			}
		}
		a[ prt ] = index + '0';
		b[ prt ] = 10 - index + '0';
		--ia[ index ];
		--ib[ 10 - index ];
		++prt;

		for( int i = 1; i < 10; ++i ){
			int qtd = min( ia[ i ], ib[ 9 - i ] );
			if( qtd > 0 ){
				for( int j = 0; j < qtd; ++j ){
					a[ prt ] = i + '0';
					b[ prt++ ] = 9 - i + '0';
				}
				ia[ i ] -= qtd;
				ib[ 9 - i ] -= qtd;
			}
		}

		int pa = prt, pb = prt;
		for( int i = 1; i < 10; ++i ){
			while( ia[ i ] ){
				a[ pa++ ] = i + '0';
				--ia[ i ];
			}

			while( ib[ i ] ){
				b[ pb++ ] = i + '0';
				--ib[ i ];
			}
		}

		while( ia[ 0 ] > ib[ 0 ] ){
			a[ pa++ ] = '0';
			--ia[ 0 ];
		}

		while( ib[ 0 ] > ia[ 0 ] ){
			b[ pb++ ] = '0';
			--ib[ 0 ];
		}
		reverse( a, a + pa );
		reverse( b, b + pb );

		while( ia[ 0 ] && ib[ 0 ] ){
			a[ pa++ ] = b[ pb++ ] = '0';
			--ia[ 0 ];
			--ib[ 0 ];
		}
		a[ pa ] = 0;
		b[ pb ] = 0;
		printf( "%s\n%s\n", a, b );
	}

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define N 200000
char a[ N ], b[ N ];
int ia[ 10 ], ib[ 10 ];
char linha[ N ];

int main(){
	scanf( "%s", linha );
	memset( ia, 0, sizeof ia );
	memset( ib, 0, sizeof ib );

	for( int i = 0; linha[i]; ++i ){
		++ia[ linha[i] - '0' ];
		++ib[ linha[i] - '0' ];
	}

	int maior = 0, index = 1;
	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = 1;
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 0; j <= 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	if( !maior ){
		int prt = strlen( linha ) - 1;
		for( int i = 0; i < 10; ++i ){
			while( ia[ i ] ){
				linha[ prt-- ] = i + '0';
				--ia[ i ];
			}
		}

		printf( "%s\n%s\n", linha, linha );
	}else{
		int prt = 0;

		a[ prt ] = index + '0';
		b[ prt ] = 10 - index + '0';
		--ia[ index ];
		--ib[ 10 - index ];
		++prt;

		for( int i = 1; i < 10; ++i ){
			int qtd = min( ia[ i ], ib[ 9 - i ] );
			if( qtd > 0 ){
				for( int j = 0; j < qtd; ++j ){
					a[ prt ] = i + '0';
					b[ prt++ ] = 9 - i + '0';
				}
				ia[ i ] -= qtd;
				ib[ 9 - i ] -= qtd;
			}
		}

		int pa = prt, pb = prt;
		for( int i = 1; i < 10; ++i ){
			while( ia[ i ] ){
				a[ pa++ ] = i + '0';
				--ia[ i ];
			}

			while( ib[ i ] ){
				b[ pb++ ] = i + '0';
				--ib[ i ];
			}
		}

		while( ia[ 0 ] > ib[ 0 ] ){
			a[ pa++ ] = '0';
			--ia[ 0 ];
		}

		while( ib[ 0 ] > ia[ 0 ] ){
			b[ pb++ ] = '0';
			--ib[ 0 ];
		}
		reverse( a, a + pa );
		reverse( b, b + pb );

		while( ia[ 0 ] && ib[ 0 ] ){
			a[ pa++ ] = b[ pb++ ] = '0';
			--ia[ 0 ];
			--ib[ 0 ];
		}
		a[ pa ] = 0;
		b[ pb ] = 0;
		printf( "%s\n%s\n", a, b );
	}

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define N 200000
char a[ N ], b[ N ];
int ia[ 10 ], ib[ 10 ];
char linha[ N ];

int main(){
	scanf( "%s", linha );
	memset( ia, 0, sizeof ia );
	memset( ib, 0, sizeof ib );

	for( int i = 0; linha[i]; ++i ){
		++ia[ linha[i] - '0' ];
		++ib[ linha[i] - '0' ];
	}

	int maior = 0, index = 1;
	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = 1;
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 0; j <= 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	for( int i = 1; i < 10; ++i ){
		if( min( ia[ i ], ib[ 10 - i ] ) > 0 ){
			int tmp = 1;
			--ia[ i ];
			--ib[ 10 - i ];

			for( int j = 1; j < 9; ++j ){
				int qtd = min( ia[ j ], ib[ 9 - j ] ) ;
				if( qtd > 0 ) tmp += qtd;
			}
			if( tmp > maior ){
				maior = tmp;
				index = -i;
			}
			++ia[ i ];
			++ib[ 10 - i ];
		}
	}

	if( !maior ){
		int prt = strlen( linha ) - 1;
		for( int i = 0; i < 10; ++i ){
			while( ia[ i ] ){
				linha[ prt-- ] = i + '0';
				--ia[ i ];
			}
		}

		printf( "%s\n%s\n", linha, linha );
	}else{
		int prt = 0;
		if( index < 0 ){
			index = -index;
			while( ia[ 0 ] ){
				a[ prt ] = '0';
				b[ prt++ ] = '0';
				--ia[ 0 ];
				--ib[ 0 ];
			}
		}
		a[ prt ] = index + '0';
		b[ prt ] = 10 - index + '0';
		--ia[ index ];
		--ib[ 10 - index ];
		++prt;

		for( int i = 1; i < 10; ++i ){
			int qtd = min( ia[ i ], ib[ 9 - i ] );
			if( qtd > 0 ){
				for( int j = 0; j < qtd; ++j ){
					a[ prt ] = i + '0';
					b[ prt++ ] = 9 - i + '0';
				}
				ia[ i ] -= qtd;
				ib[ 9 - i ] -= qtd;
			}
		}

		int pa = prt, pb = prt;
		for( int i = 1; i < 10; ++i ){
			while( ia[ i ] ){
				a[ pa++ ] = i + '0';
				--ia[ i ];
			}

			while( ib[ i ] ){
				b[ pb++ ] = i + '0';
				--ib[ i ];
			}
		}

		while( ia[ 0 ] > ib[ 0 ] ){
			a[ pa++ ] = '0';
			--ia[ 0 ];
		}

		while( ib[ 0 ] > ia[ 0 ] ){
			b[ pb++ ] = '0';
			--ib[ 0 ];
		}
		reverse( a, a + pa );
		reverse( b, b + pb );

		while( ia[ 0 ] && ib[ 0 ] ){
			a[ pa++ ] = b[ pb++ ] = '0';
			--ia[ 0 ];
			--ib[ 0 ];
		}
		a[ pa ] = 0;
		b[ pb ] = 0;
		printf( "%s\n%s\n", a, b );
	}

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cctype>
#include <numeric>
#include <queue>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <iterator>
#define FOR(i,s,e) for(int i=(s);i<(int)(e);i++)
#define FOE(i,s,e) for(int i=(s);i<=(int)(e);i++)
#define ALL(x) (x).begin(), (x).end()
#define CLR(s) memset(s,0,sizeof(s))
#define PB push_back
#define ITER(v)      __typeof((v).begin())
#define FOREACH(i,v) for(ITER(v) i=(v).begin();i!=(v).end();i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef map<int,int> mii;
typedef vector<int> vi;
#define x first
#define y second

int bin[10] = {0};
string opt1, opt2;
int ans = -1;

int main() {
        char c;
        while ( (c=getchar()) != EOF && isdigit(c) )
                bin[c - '0'] ++;

        string tmp = "";
        FOR(i, 0, 10) FOR(j, 0, bin[i]) tmp += (char)('0' + i);
        opt1 = opt2 = tmp;
        ans  = 0;

        // exhaust . . .
        FOE(x, 0, 9) {
                int y = x ? 10 - x : 0;
                if (bin[x] > 0 && bin[y] > 0) {
                        // pad zero
                        {
                                int as[10], at[10];
                                memcpy(as, bin, sizeof(as));
                                memcpy(at, bin, sizeof(at));
                                as[x]--; at[y]--;
                                as[0] = at[0] = 0;

                                int cur = bin[0] + 1;
                                string s, t;
                                FOR(j, 0, bin[0]) {
                                        s += '0';
                                        t += '0';
                                }
                                s += (char)('0'+x);
                                t += (char)('0'+y);

                                FOE(i, 1, 9) {
                                        while (as[i] > 0 && at[9-i] > 0) {
                                                s += (char)('0'+i);
                                                t += (char)('0'+(9-i));
                                                as[i]--;
                                                at[9-i]--;
                                                cur++;
                                        }
                                }
                                if (cur > ans) {
                                        ans = cur;
                                        FOE(i, 0, 9) FOR(j, 0, as[i]) s += (char)('0'+i);
                                        FOE(i, 0, 9) FOR(j, 0, at[i]) t += (char)('0'+i);
                                        opt1 = s;
                                        opt2 = t;
                                }
                        }
                        // don't pad zero
                        {
                                int as[10], at[10];
                                memcpy(as, bin, sizeof(as));
                                memcpy(at, bin, sizeof(at));
                                as[x]--; at[y]--;

                                int cur = 1;
                                string s, t;
                                s += (char)('0'+x);
                                t += (char)('0'+y);

                                FOE(i, 0, 9) {
                                        while (as[i] > 0 && at[9-i] > 0) {
                                                s += (char)('0'+i);
                                                t += (char)('0'+(9-i));
                                                as[i]--;
                                                at[9-i]--;
                                                cur++;
                                        }
                                }

                                while (as[0] > 0 && at[0] > 0) {
                                        as[0]--; at[0]--;
                                        s += '0';
                                        t += '0';
                                        cur++;
                                }

                                if (cur > ans) {
                                        ans = cur;
                                        FOE(i, 0, 9) FOR(j, 0, as[i]) s += (char)('0'+i);
                                        FOE(i, 0, 9) FOR(j, 0, at[i]) t += (char)('0'+i);
                                        opt1 = s;
                                        opt2 = t;
                                }

                        }
                }
        }
        reverse(ALL(opt1));
        reverse(ALL(opt2));
        cout << opt1 << endl << opt2 << endl;
        return 0;
}