#include using namespace std; #define rep(i,n) REP(i,0,n) #define REP(i,s,e) for(int i=(s); i<(int)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--) #define pb push_back #define all(r) r.begin(),r.end() #define rall(r) r.rbegin(),r.rend() #define fi first #define se second typedef long long ll; typedef vector vi; typedef vector vl; typedef pair pii; typedef pair pll; const int INF = 1e9; const ll MOD = 1e9 + 7; double EPS = 1e-8; #define DEBUG_MODE #ifdef DEBUG_MODE #define dump(x) cout << #x << " : " << x << endl #define LINE cout << "line : " << __LINE__ << endl #define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl #define STOP assert(false) #else #define dump(x) ; #define LINE ; #define dumpV(v); #define STOP ; #endif #define mp make_pair namespace std{ template ostream &operator <<(ostream& out,const pair& a){ out<<'('< primes; vector isPrime(MAX_N, 1); void buildPrimes() { isPrime[0] = isPrime[1] = 0; REP(i, 2, MAX_N) { if(isPrime[i]) { primes.push_back(i); for(int j = i+i; j < MAX_N; j += i) isPrime[j] = false; } } } int main(){ buildPrimes(); int n; cin >> n; vector v(10); rep(i, n) { int a; cin >> a; v[a]++; } int l = 1, r = 3, ans = -1; vector cnt(10, 0); cnt[2] = 1; constexpr int MAX = 5e6; auto check = [&]() { rep(i, 10) { if(v[i] > 0 && cnt[i] == 0) return 1; if(v[i] == 0 && cnt[i] > 0) return -1; } return 0; }; auto update = [&](int n, bool plus) { int x = n; while(x > 0) { if(plus) cnt[x%10]++; else cnt[x%10]--; x /= 10; } }; while(r <= MAX) { // dump(mp(l, r)); // dumpV(cnt); if(check() == 0) ans = max(ans, r - l-1); if(check() >= 0) { if(isPrime[r]) update(r, true); r++; } else { if(isPrime[l]) update(l, false); ++l; if(l == r) { auto it = lower_bound(all(primes), r); if(it == primes.end()) break; r = *it; update(r, true); ++r; } } } if(check() == 0) ans = max(ans, r - l-1); cout << ans << endl; return 0; }