#include #include #include #include #include #include #include #include #include #include #include #include typedef long long ll; using namespace std; #define mod 1000000007 #define INF 1000000000 #define LLINF 2000000000000000000LL #define MAX_L 5000000 #define SIZE 1000000 vector pnum; const int MAX_P = 5000000; bool prime[MAX_P+1]; void primen(void){ for(int i=2;i<=MAX_P;i++) prime[i]=true; for(int i=2;i*i<=MAX_P;i++) if(prime[i]) for(int j=i;i*j<=MAX_P;j++) prime[i*j]=false; } int main(){ int n,k,A,ans=-1,a,f; int m[10]={0},m_in[10]={0}; scanf("%d",&n); primen(); pnum.push_back(0); for(int i=2;i<=MAX_L;i++){ if(prime[i]==true){ pnum.push_back(i); } } pnum.push_back(5000001); for(int i=0;i0) f = true; } if(f){ a = pnum[k]; while(a){ m[a%10]--; a/=10; } k++; }else{ break; } } bool f = true; for(int i=0;i<10;i++){ if(m_in[i]>0 && m[i]==0) f = false; } if(f){ ans = max(ans,(pnum[l]-1)-(pnum[k-1]+1)); } a = pnum[l]; while(a){ m[a%10]++; a/=10; } } printf("%d\n",ans); return 0; }