#include #include #include #include using namespace std; #define MAX 5000000 #define N_MAX 10 int N; int A[N_MAX]; bool isnotprime[MAX + 1]; vector> prime; void make_prime() { isnotprime[0] = isnotprime[1] = 1; for (int i = 2; i * i <= MAX; i++) { if (!isnotprime[i]) { for (int j = i * i; j <= MAX; j += i) { isnotprime[j] = 1; } } } prime.emplace_back(-1, 0); for (int i = 2; i <= MAX; i++) { if (isnotprime[i]) continue; prime.emplace_back(i, 0); } prime.emplace_back(MAX, 0); } void make_mask() { for (int i = 1; i + 1 < prime.size(); i++) { int x = prime[i].first; int mask = 0; while (x) { mask |= 1 << x % 10; x /= 10; } prime[i].second = mask; } } int main() { make_prime(); make_mask(); scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d", A + i); if (N == 10) { printf("%d\n", MAX - 1); return 0; } int mask = 0; for (int i = 0; i < N; i++) mask |= 1 << A[i]; int res = -1; for (int i = 1; i + 1 < prime.size(); i++) { int bit = 0; for (int j = i; j + 1 < prime.size(); j++) { bit |= prime[j].second; if ((bit | mask) != mask) break; if (bit == mask) { res = max(res, (prime[j + 1].first - 1) - (prime[i - 1].first + 1)); } } } printf("%d\n", res); return 0; }