#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 = -1; unordered_map um; for(int i = 0; i < pn; i++) { int t = v[i]; while(t > 0) { int md = t % 10; if(!a[md]) { if(um.size() == n && (i != k + 1 || k == -1)) { if(k == -1) res = v[i] - 2; else { res = max(res, v[i] - v[k] - 2); } } k = i; um.clear(); break; } t /= 10; um[md] = true; } } if(um.size() == n && v.size() != k + 1) if(k == -1) res = v[v.size() - 1]; else res = max(res, v[v.size() - 1] - v[k] - 2); cout << res << endl; return 0; }