import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.List; public class Main { int n; int[] a; int set(int k) { int ret = 0; while (0 < k) { ret |= (1 << k % 10); k /= 10; } return ret; } void run() { MyScanner sc = new MyScanner(); int T = 5000000 + 1; // int T = 100; boolean[] prime = new boolean[T]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for (int i = 0; i * i <= T; i++) { if (prime[i]) { for (int j = i * i; j < T; j += i) { prime[j] = false; } } } List tmp = new LinkedList(); tmp.add(0); for (int i = 0; i < T; i++) { if (prime[i]) { tmp.add(i); } } tmp.add(T); Integer[] list = tmp.toArray(new Integer[0]); int[] bit = new int[list.length]; for (int i = 1; i < list.length - 1; i++) { bit[i] = set(list[i]); } int n = sc.nextInt(); int mask = 0; for (int i = 0; i < n; i++) { mask |= (1 << sc.nextInt()); } if (n < 10) { int max = -1; // System.out.println("mask: " + Integer.toBinaryString(mask)); for (int i = 1; i < list.length - 1; i++) { // System.out.println("i: " + list[i]); int set = 0; for (int j = i; j < list.length - 1; j++) { set |= bit[j]; // System.out.println(" set: " + // Integer.toBinaryString(set)); if (set == mask) { max = Math.max(max, (list[j + 1] - 1) - (list[i - 1] + 1)); } if ((set & mask) != set) { break; } } } System.out.println(max); } else { System.out.println(4999999); } } public static void main(String[] args) { new Main().run(); } public void mapDebug(int[][] a) { System.out.println("--------map display---------"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.printf("%3d ", a[i][j]); } System.out.println(); } System.out.println("----------------------------" + '\n'); } class MyScanner { int read() { try { return System.in.read(); } catch (IOException e) { throw new InputMismatchException(); } } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }