#include "bits/stdc++.h" using namespace std; typedef long long Int; #define REP(i,n) for(int (i)=0;(i)<(int)(n);++(i)) const int MAX_N = 5000000; bool is_prime[MAX_N]; vector primes; vector dis(int p) { vector res(10); while (p > 0) { res[p % 10]++; p /= 10; } return res; } int main() { for (int i = 0; i < MAX_N; i++) is_prime[i] = true; is_prime[0] = is_prime[1] = false; for (int i = 2; i * i <= MAX_N; i++) if (is_prime[i]) for (int j = i * i; j < MAX_N; j += i) is_prime[j] = false; primes.push_back(0); for (int i = 0; i < MAX_N; i++) { if (is_prime[i]) primes.push_back(i); } primes.push_back(5000001); vector target(10); int N; cin >> N; REP(i, N) { int x; cin >> x; target[x]++; } vector current(10); int left = 1, right = 1; int ans = -1; int LIM = primes.size() - 1; while (right < LIM || left < LIM) { bool fast = false; bool slow = false; REP(i, 10) { if (current[i] > 0 && target[i] == 0) fast = true; if (current[i] == 0 && target[i] > 0) slow = true; } if (!fast && !slow) { ans = max(ans, (primes[right] - 1) - (primes[left-1] + 1)); } if (right >= LIM || fast) { vector v = dis(primes[left]); REP(i, 10) current[i] -= v[i]; left++; } else { vector v = dis(primes[right]); REP(i, 10) current[i] += v[i]; right++; } } cout << ans << endl; }