package no012; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); boolean[] use = new boolean[10]; for(int i=0;i primes = Sieve.primeList(5000000); int j = 0; int ans = -1; for(int i=0;i 0) { expand = false; ok = false; break; } if (use[k] && digits[k] == 0) { ok = false; } } if (ok) { int k = i == 0 ? 1 : primes.get(i-1) + 1; int l = j == primes.size() ? 5000000 : primes.get(j) - 1; ans = Math.max(ans,l-k); } if (!expand) { break; } if (j == primes.size()) { break; } int p = primes.get(j); while(p > 0) { digits[p%10]++; p/=10; } //System.out.println(primes.get(i) + "," + primes.get(j)); j++; } int p = primes.get(i); while(p > 0) { digits[p%10]--; p/=10; } } System.out.println(ans); } } class Sieve { public static boolean[] isPrimeArray(int max) { boolean[] isPrime = new boolean[max+1]; Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for(int i=2;i*i<=max;i++) { if (isPrime[i]) { int j = i * 2; while(j<=max) { isPrime[j] = false; j += i; } } } return isPrime; } public static ArrayList primeList(int max) { boolean[] isPrime = isPrimeArray(max); ArrayList primeList = new ArrayList<>(); for(int i=2;i<=max;i++) { if (isPrime[i]) { primeList.add(i); } } return primeList; } }