#include #include #include #include #include using namespace std; int MakePrime(vector& v, int n) { bool p[5000001] = {1, 1}; for(int i = 2; i <= n; i++) { if(p[i]) continue; v.push_back(i); for(int j = i * 2; j <= n; j += i) p[j] = true; } return v.size(); } int main(void) { const int maxdp = 20001; int n; vector v; int pn = MakePrime(v, 5000000); bool a[10] = {0}; cin >> n; for(int i = 0; i < n; i++) { int z; cin >> z; a[z] = true; } int res = -1; int k = 0; unordered_map um; for(int i = 0; i < pn; i++) { int t = v[i]; bool find=false; while(t > 0) { int md = t % 10; if(!a[md]) { if(um.size() == n && (i != k + 1 || k == 0)) { if(k == 0) res = v[i] - 2; else { res = max(res, v[i] - v[k] - 2); } } k = i; find=true; um.clear(); break; } t /= 10; } if(!find){ t = v[i]; while(t > 0) { int md = t % 10; um[md] = true; t/=10; } } } if(um.size() == n && v.size() != k + 1) if(k == 0) res = v[v.size() - 1]; else res = max(res, v[v.size() - 1] - v[k] - 2); cout << res << endl; return 0; }