#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=2;i<10000;i++){ if(isp[i])continue; if(ans[i])continue; queue q; int m=0; for(int j=1;i+j<10000;j++){ if(!isp[i+j]&&!ans[i+j]){m=j;q.push(i+j);break;} } while(q.size()){ ans[a=q.front()]=m; q.pop(); if(a+1<10000&&!isp[a+1]&&!ans[a+1]){ans[a+1]=m;q.push(a+1);} if(a+m<10000&&!isp[a+m]&&!ans[a+m]){ans[a+m]=m;q.push(a+m);} if(a-1>1&&!isp[a-1]&&!ans[a-1]){ans[a-1]=m;q.push(a-1);} if(a-m>1&&!isp[a-m]&&!ans[a-m]){ans[a-m]=m;q.push(a-m);} } } if(n>=10000)n=5000; printf("%d\n",ans[n]); return 0; }