import java.util.ArrayList; import java.util.BitSet; import java.util.List; import java.util.Scanner; public class Main_yukicoder12 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = 5_000_000; List primes = new ArrayList(); BitSet sieve = new BitSet(); primes.add(0); primes.add(2); for (int i = 1; 2 * i + 1 <= n; i++) { if (!sieve.get(i)) { int p = 2 * i + 1; primes.add(p); for (int j = 1; (i + (2 * i + 1) * j) * 2 + 1 <= n; j++) { sieve.set(i + (2 * i + 1) * j); } } } primes.add(5_000_001); n = sc.nextInt(); int mask = 0; for (int i = 0; i < n; i++) { int a = sc.nextInt(); mask |= 0x1 << a; } int prev = -1; int omask = 0; int max = -1; for (int i = 0; i < primes.size(); i++) { int p = primes.get(i); if (p == 0 || p == 5_000_001) { continue; } int tmp = 0; while (p > 0) { tmp |= 0x1 << p % 10; p /= 10; } if ((tmp | mask) == mask) { if (prev == -1) { prev = i; omask = tmp; } else { omask |= tmp; } if ((omask & mask) == mask) { max = Math.max(max, primes.get(i + 1) - 1 - (primes.get(prev - 1) + 1)); } } else { prev = -1; } } System.out.println(max); sc.close(); } }