#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef _MSC_VER #include #endif #define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end()) #define MP make_pair #define MT make_tuple using namespace std; typedef long long ll; typedef pair P; const int N = 5e6+1; int np[N]; int need[10]; int cnt[10]; bool can(){ rep(i, 10) if (!need[i] && cnt[i]) return false; return true; } bool check(){ rep(i, 10) if (need[i] != (cnt[i] >= 1)) return false; return true; } void f(int val, int k){ if (!val) return; cnt[val % 10] += k; f(val / 10, k); } int main(){ cin.tie(0); ios::sync_with_stdio(false); for (int i = 2; i*i < N; ++i){ if (!np[i]) for (int j = i * 2; j < N; j += i) np[j] = 1; } int n; cin >> n; rep(i, n){ int x; cin >> x; need[x] = 1; } int l = 1, r = 1; int ans = -1; while (1){ while (r < N && can()){ if(check()) ans = max(ans, r - l - 1); if (!np[r]) f(r, 1); ++r; } if (check()) ans = max(ans, r - l - 1); if (l >= r) break; if (!np[l]) f(l, -1); ++l; if(check()) ans = max(ans, r - l - 1); } cout << ans << endl; return 0; }