#include<bits/stdc++.h> using namespace std; string a,m,s,t; int x,c,b,b1; char r; int minn=2147483647; bool pd(int n){ for(int i=2;i*i<=n;i++){ if(n%i==0) return 0; } return 1; } void fun(){ while(s.size()!=5){ r=x%10+'0'; t=""; t+=r; s=t+s; x/=10; } } int main(){ cin>>a; for(int i=99999;i>=2;i--){ if(!pd(i)){ continue; } s="",x=i,c=0; fun(); for(int j=0;j<5;j++){ b=0,b=1; if(s[j]==a[j]){ continue; } if(s[j]>a[j]){ b=s[j]-a[j]; b1=9-s[j]+1+a[j]; }else{ b=a[j]-s[j]; b1=s[j]+1+9-a[j]; } c+=min(b,b1); } if(c<minn){ m=s; minn=c; } } cout<<m; return 0; }```cpp
```cpp
#include<bits/stdc++.h>
using namespace std;
string a,m,s,t;
int x,c,b,b1;
char r;
int minn=2147483647;
bool pd(int n){
for(int i=2;i*i<=n;i++){
if(n%i==0) return 0;
}
return 1;
}
void fun(){
while(s.size()!=5){
r=x%10+'0';
t="";
t+=r;
s=t+s;
x/=10;
}
}
int main(){
cin>>a;
for(int i=99999;i>=2;i--){
if(!pd(i)){
continue;
}
s="",x=i,c=0;
fun();
for(int j=0;j<5;j++){
b=0,b=1;
if(s[j]==a[j]){
continue;
}
if(s[j]>a[j]){
b=s[j]-a[j];
b1=9-s[j]+1+a[j];
}else{
b=a[j]-s[j];
b1=s[j]+1+9-a[j];
}
c+=min(b,b1);
}
if(c<minn){
m=s;
minn=c;
}
}
cout<<m;
return 0;
}