#include #include #include #include #include #include #include #include #include #include #include using namespace std; #define int long long #define FOR(i, a, b) for(int i=(a);i<(b);i++) #define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--) #define REP(i, n) for(int i=0; i<(n); i++) #define RREP(i, n) for(int i=(n-1); i>=0; i--) #define ALL(a) (a).begin(),(a).end() #define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end()); #define CONTAIN(a, b) find(ALL(a), (b)) != (a).end() #define SUM(a) accumulate(ALL(a), 0) #define array2(type, x, y) array, x> #define vector2(type) vector > #define out(...) printf(__VA_ARGS__) typedef pair pos; int pos::*x = &pos::first; int pos::*y = &pos::second; int dxy[] = {0, 1, 0, -1, 0}; /*================================*/ // 5000000 bool notPrime[5000010]; bool ok[10]; int N; set separate(int n) { set s; while (n > 0) { s.insert(n % 10); n /= 10; } return s; } bool isok(int icnt[10]) { REP(i, 10) { if (ok[i] && !icnt[i]) return false; if (!ok[i] && icnt[i]) return false; } return true; } bool isover(set s) { for (int t : s) { if (!ok[t]) return true; } return false; } signed main() { notPrime[0] = notPrime[1] = true; FOR(i,2,5000001) { if (notPrime[i] == false) { int k = 2; while (i*k <= 5000000) { notPrime[i*k] = true; k++; } } } cin >> N; int c; REP(i,N) { cin >> c; ok[c] = true; } int maxl = -1; int icnt[10] = {0}; int i = 1; int j = 1; while (j <= 5000000) { if (!notPrime[j]) { set s = separate(j); if (isover(s)) { if (isok(icnt)) { maxl = max(maxl, j - i - 1); } REP(k,10) icnt[k] = 0; j++; i = j; continue; } else { for (auto& t : s) icnt[t]++; } } j++; } if (isok(icnt)) { maxl = max(maxl, j - i - 1); } cout << maxl << endl; return 0; }