#include using namespace std; int p[5000010]; vector t(10); void f(int n){ if( n == 0 ){ t = vector(10); return; } f(n/10); t[n%10]++; return; } int bit = 0; bool noChance(vector c){ for(int i = 0 ; i < 10 ; i++){ if( !(bit>>i&1) && c[i] ){ return true; } } return false; } bool ok(vector c){ for(int i = 0 ; i < 10 ; i++){ if( (bit>>i&1) ^ (!!c[i]) ){ return false; } } return true; } int main(){ for(int i = 2 ; i <= 5000000 ; i++) p[i] = 1; int N; cin >> N; for(int i = 0 ; i < N ; i++) { int x; cin >> x; bit |= (1< now(10); vector P,Q; for(long long i = 2 ; i <= 5000000 ; i++){ if( p[i] ){ P.push_back(i); for(long long j = i*i ; j <= 5000000 ; j += i ) p[j] = 0; } } int j = 0; int ans = -1; for(int i = 0 ; i < P.size() ; i++){ while(j < P.size()){ f(P[j]); vector tnow = now; for(int j = 0 ; j < 10 ; j++) tnow[j] += t[j]; if( noChance(tnow) ) break; now = tnow; j++; } if( i == j ){ j++; }else{ if(ok(now)){ int A = P[i]; int B = P[j-1]; while( B < 5000000 && !p[B+1] ) B++; while( A > 1 && !p[A-1] ) A--; // if( B - A == 158 || B - A == 166 ) // cout << "(" << B-A << ")" << A << " " << B << "|" << P[i] << " " << P[j] << endl; ans = max(ans,B-A); } f(P[i]); for(int j = 0 ; j < 10 ; j++) now[j] -= t[j]; } } cout << ans << endl; }