#include #include #include #include using namespace std; int main(){ char s[31]; scanf("%s",s); int len=strlen(s); int n; if(len>2){printf("8\n");return 0;} else n=atoi(s); int isp[10001]={}; isp[2]=1; for(int i=3;i<=n;i+=2)isp[i]=1; for(int i=3;i*i<=n;i+=2){ if(isp[i])for(int j=i*i;j<=n;j+=i)isp[j]=0; } int ans[10001]={}; ans[1]=1; int a=0; for(int i=1;i q;q.push(1+i); int b[10001]={}; while(q.size()){ if(!ans[a=q.front()])ans[a]=i; q.pop(); if(a+1<=n&&!isp[a+1]&&!b[a+1]){b[a+1]++;q.push(a+1);} if(a+i<=n&&!isp[a+i]&&!b[a+i]){b[a+i]++;q.push(a+i);} if(a-1>1&&!isp[a-1]&&!b[a-1]){b[a-1]++;q.push(a-1);} if(a-i>1&&!isp[a-i]&&!b[a-i]){b[a-i]++;q.push(a-i);} } } /*for(int i=0;i<20;i++){ if(isp[i])printf("%d ",i); } for(int i=0;i<20;i++)printf("%d %d \n",ans[i],i); */ if(n>=10000)n=5000; printf("%d\n",ans[n]); return 0; }