//#include #include using namespace std; typedef long long ll; typedef pair p; const int INF = 1e9; const ll LINF = ll(1e18); const int MOD = 1000000007; const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(v) v.begin(), v.end() #define debug(v) \ cout << #v << ":"; \ for (auto x : v) \ { \ cout << x << ' '; \ } \ cout << endl; template bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } //cout< a; //エラトステネスの篩 vector sieve(int n) { vector isp(n + 1, true); isp[0] = false; isp[1] = false; for (int i = 2; pow(i, 2) <= n; i++) { if (isp[i]) for (int j = 2; i * j <= n; j++) isp[i * j] = false; } return isp; } void change(vector &count, int k, int dx) { while (k) { count[k % 10] += dx; k /= 10; } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; vector must(10, false); for (int i = 0; i < n; i++) { int temp; cin >> temp; a.push_back(temp); must[temp] = true; } vector isp = sieve(5000000); vector count(10, 0); int i = 1; int j = 1; int ans = -1; while (j <= 5000000) { if (isp[j]) { change(count, j, 1); } bool check = false; do { rep(k, 10) { if (!must[k] && count[k]) { //余分なのがいる check = true; } } if (check) { while (!isp[i] && i < j) i++; change(count, i, -1); //左の素数の分引く i++; } } while (check && i < j); //余分な数字はなくなりました bool check1 = true; rep(k, 10) { if (must[k] && !count[k]) { check1 = false; //必要なものがない } } if (check1) { chmax(ans, j - i); } j++; } cout << ans << "\n"; }