#include<bits/stdc++.h>
using namespace std;
long long n,a1,a2,a3,a4,a5,f[1000100],s1,s2,s3,s4,s5,x1,x2,x3,x4,x5,x,minn=INT_MAX,ans;
int main( )
{
cin>>n;
a1=n/10000;
a2=n/1000%10;
a3=n/100%10;
a4=n/10%10;
a5=n%10;
f[1]=1;
for(int i=2;i<=sqrt(100000);i++)
{
if(f[i]==0)
{
for(int j=1;j<=100000/i;j++)
{
f[i+i*j]=1;
}
}
}
for(int i=1;i<=100000;i++)
{
if(f[i]==0)
{
s1=i/10000;
s2=i/1000%10;
s3=i/100%10;
s4=i/10%10;
s5=i%10;
x1=abs(s1-a1);
x1=min(x1,min(s1+10-a1,a1+10-s1));
x2=abs(s2-a2);
x2=min(x2,min(s2+10-a2,a2+10-s2));
x3=abs(s3-a3);
x3=min(x3,min(s3+10-a3,a3+10-s3));
x4=abs(s4-a4);
x4=min(x4,min(s4+10-a4,a4+10-s4));
x5=abs(s5-a5);
x5=min(x5,min(s5+10-a5,a5+10-s5));
ans=x1+x2+x3+x4+x5;
if(ans<=minn)
{
minn=ans;
x=i;
}
}
}
if(x/10000==0)
{
cout<<0<<x;
return 0;
}
if(x/1000==0)
{
cout<<00<<x;
return 0;
}
if(x/100==0)
{
cout<<000<<x;
return 0;
}
if(x/10==0)
{
cout<<0000<<x;
return 0;
}
cout<<x;
return 0;
}