#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int target = 0; for (int i = 0; i < n; ++i) { int a; cin >> a; target |= 1 << a; } const int LIM = 5000000; vector isPrime(LIM + 1, true); isPrime[0] = isPrime[1] = false; for (int i = 2; 1LL * i * i <= LIM; ++i) { if (!isPrime[i]) continue; for (int j = i * i; j <= LIM; j += i) { isPrime[j] = false; } } int ans = -1; // 直前の禁止素数 int lastBad = 0; // lastBad より後にある、使用可能な素数の数字集合 int used = 0; for (int p = 2; p <= LIM; ++p) { if (!isPrime[p]) continue; int mask = 0; int x = p; while (x > 0) { mask |= 1 << (x % 10); x /= 10; } // target に含まれない数字が使われている if (mask & ~target) { // 区間 [lastBad + 1, p - 1] if (used == target) { ans = max(ans, p - lastBad - 2); } lastBad = p; used = 0; } else { used |= mask; } } // 最後の禁止素数から 5000000 まで if (used == target) { ans = max(ans, LIM - lastBad - 1); } cout << ans << '\n'; }