#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; void findPrime(int N, vector& isPrime) { isPrime.assign(N+1, true); isPrime[0] = isPrime[1] = false; for(int i=2; i*i<=N; i++){ if(isPrime[i]){ for(int j=i; i*j<=N; j++){ isPrime[i*j] = false; } } } } void addCnt(int x, vector& cnt) { ostringstream oss; oss << x; string s = oss.str(); for(unsigned i=0; i& cnt) { ostringstream oss; oss << x; string s = oss.str(); for(unsigned i=0; i isPrime; findPrime(5000000, isPrime); int n; cin >> n; bitset<10> select; for(int i=0; i> a; select[a] = true; } int left = 1; int right = 0; int ret = -1; vector cnt(10, 0); for(;;){ bool isNeedAdd = false; bool isNeedSub = false; for(int i=0; i<10; ++i){ if(select[i] && cnt[i] == 0) isNeedAdd = true; if(!select[i] && cnt[i] > 0) isNeedSub = true; } if(!isNeedAdd && !isNeedSub){ ret = max(ret, right - left); } if(isNeedSub){ if(isPrime[left]) subCnt(left, cnt); ++ left; } else{ ++ right; if(right > 5000000) break; if(isPrime[right]) addCnt(right, cnt); } } cout << ret << endl; return 0; }