#include using namespace std; #define rep(i,n) for (int i = 0; i < n; ++i) using ll = long long; using P = pair; bool subset(set main, set sub){ for (auto &&i : sub) if(main.find(i)==main.end()) return false; return true; } bool equalset(sets1,sets2){ if(s1.size()!=s2.size())return false; return (subset(s1,s2)); } // Sieve of Eratosthenes struct Sieve { int n; vector f, primes; Sieve(int n=1):n(n), f(n+1) { f[0] = f[1] = -1; for (ll i = 2; i <= n; ++i) { if (f[i]) continue; primes.push_back(i); f[i] = i; for (ll j = i*i; j <= n; j += i) { if (!f[j]) f[j] = i; } } } bool isPrime(int x) { return f[x] == x;} vector factorList(int x) { vector res; while (x != 1) { res.push_back(f[x]); x /= f[x]; } return res; } vector

factor(int x) { vector fl = factorList(x); if (fl.size() == 0) return {}; vector

res(1, P(fl[0], 0)); for (int p : fl) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } }; set num_to_nums(int num){ set nums; while(num>0){ nums.insert(num%10); num/=10; } return nums; } int main() { Sieve primes(5000000); int N;cin>>N; set now,target; int l=1,r=1,ans=0; rep(i,N){ int Ai;cin>>Ai; target.insert(Ai); } primes.primes.push_back(5000001); for (int i = 0; i < primes.primes.size()-1; i++) { now.merge(num_to_nums(primes.primes[i])); if(subset(target,now)){ if(equalset(target,now)) r=primes.primes[i+1]-1; } else{ l=primes.primes[i]+1; now.clear(); } ans=max(ans,r-l); } cout<<(ans==0?-1:ans)<