#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define mp make_pair #define mt make_tuple #define pb push_back #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair pii; const int INF=1<<29; const double EPS=1e-9; const int dx[]={1,0,-1,0},dy[]={0,-1,0,1}; const int MAX_L = 5000010; int N; vector digit; vector prime; bool is_prime[MAX_L]; bool ok[11]; void sieve(){ is_prime[0] = is_prime[1] = false; for (ll i = 2; i * i < MAX_L; i++){ if (is_prime[i]){ for (ll j = i + i; j < MAX_L; j += i){ is_prime[j] = false; } } } for (int i = 2; i <= 5000000; i++){ if (is_prime[i]){ prime.push_back(i); } } } bool check(){ for (int i = 0; i < N; i++){ if (ok[i]){ }else{ return false; } } return true; } bool in_digit(int x){ while (x){ int y = x % 10; bool flag = false; for (int j = 0; j < N; j++){ if (digit[j] == y){ ok[j] = true; flag = true; break; } } if (!flag)return false; x /= 10; } return true; } int main(){ cin >> N; digit.resize(N); memset(is_prime, true, sizeof(is_prime)); for (int i = 0; i < N; i++){ cin >> digit[i]; } sieve(); int K = 1; int L = 1; memset(ok, false, sizeof(ok)); int ans = -1; for (int i = 0; i < prime.size(); i++){ if (in_digit(prime[i])){ }else{ K = prime[i] + 1; L = prime[i] + 1; memset(ok, false, sizeof(ok)); } if (check()){ if (i + 1 < prime.size()){ L = prime[i + 1] - 1; }else{ L = 5000000; } if (ans < L - K){ ans = L - K; } } } cout << ans << endl; return 0; }