#include #include #include #include #define REP(i, a, b) for (int i = int(a); i < int(b); i++) #define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl using namespace std; typedef long long int lli; template vector make_v(size_t a, T b) { return vector(a, b); } template auto make_v(size_t a, Ts... ts) { return vector(a, make_v(ts...)); } int main() { int N; cin >> N; const int P = 5000001; //const int P = 51; vector isPrime(P, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i < P; i++) { if (isPrime[i]) { for (int j = 2 * i; j < P; j += i) { isPrime[j] = false; } } } auto Pd = make_v(P, 10, 0); REP(i, 0, P) { if (isPrime[i]) { int n = i; while (n) { Pd[i][n % 10]++; n /= 10; } } } REP(i, 1, P) { REP(j, 0, 10) { Pd[i][j] += Pd[i - 1][j]; } } int ans = -1; vector A(10, false); REP(i, 0, N) { int a; cin >> a; A[a] = true; } int l = 0; while (l < P) { int r = l + 1; bool isPm = false; while (r < P) { bool ok = true; REP(i, 0, 10) { if (!A[i]) { ok &= (Pd[r][i] == Pd[l][i]); } } if (ok) { isPm |= isPrime[r]; r++; } else { break; } } REP(i, 0, 10) { if (A[i]) { isPm &= (Pd[r - 1][i] > Pd[l][i]); } } if (isPm) { ans = max(ans, r - l - 2); } l = r; } cout << ans << endl; return 0; }