#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>
typedef long long LL;
using namespace std;
int prim[9600], vis[100005], tot, num[6], nu[6];
char s[6];
void get_prim(){
for (int i = 2; i < 100000; i++){
if(!vis[i]) prim[tot++] = i;
for (int j = 0; j < tot && prim[j] * i < 100000; j++){
vis[prim[j] * i] = 1;
if(i % prim[j] == 0) break;
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
get_prim();
scanf("%s", s);
for (int i = 0; i < 5; i++) num[i] = s[i] - '0';
int tim = 0x3f3f3f3f, ans;
for (int i = 0; i < tot; i++){
int cnt = 4, t = prim[i], w = 0;
while(t){
nu[cnt--] = t % 10;
t /= 10;
}
while(cnt >= 0) nu[cnt--] = 0;
for (int i = 0; i < 5; i++){
int t1 = nu[i] - num[i], t2 = num[i] - nu[i];
if(t1 < 0) t1 += 10;
if(t2 < 0) t2 += 10;
w += min(t1, t2);
}
if(tim >= w){
tim = w;
ans = prim[i];
}
}
int t = ans, cnt = 0;
while(t) cnt++, t /= 10;
for (int i = 1; i <= 5 - cnt; i++) printf("0");
printf("%d\n", ans);
return 0;
}