#include #include #include #include #include #include using namespace std; int main() { const int limit = 5000000; int n; cin >> n; bool selected[10] = {}; for (int i = 0; i < n; i++) { int a; cin >> a;; selected[a] = true; } vector isPrime(limit + 1, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= limit; i++) { if (isPrime[i]) { for (int j = 2 * i; j <= limit; j += i) { isPrime[j] = false; } } }; // 0, limit + 1 は素数ではないが、計算を楽にするために挿入 vector primes; primes.push_back(0); for (int i = 0; i <= limit; i++) { if (isPrime[i]) { primes.push_back(i); } } primes.push_back(limit+1); int np = primes.size(); set check; int ans = -1; int start = -1; // i = 0, np - 1は計算を楽にするために挿入しただけなので除く for (int i = 1; i < np - 1; i++) { int tmp = primes[i]; bool ok = true; while (tmp > 0) { int digit = tmp % 10; if (!selected[digit]) { ok = false; break; } tmp /= 10; } if (!ok) { check.clear(); continue; } if (check.size() == 0) { start = i; } tmp = primes[i]; while (tmp > 0) { check.insert(tmp % 10); tmp /= 10; } if (check.size() == n) { ans = max(ans, primes[i + 1] - primes[start - 1] - 2); } } cout << ans << endl; return 0; }