import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int target = 0; for (int i = 0; i < n; i++) { target += (1 << sc.nextInt()); } ArrayList primes = new ArrayList<>(); primes.add(0); boolean[] isNotPrimes = new boolean[5000001]; for (int i = 2; i < isNotPrimes.length; i++) { if (!isNotPrimes[i]) { primes.add(i); for (int j = 2; j * i < isNotPrimes.length; j++) { isNotPrimes[j * i] = true; } } } primes.add(5000001); int left = 1; int[] counts = new int[10]; int ans = -1; for (int i = 1; i < primes.size() - 1; i++) { addCount(counts, primes.get(i)); int current = getMask(counts); if (current == target) { ans = Math.max(ans, primes.get(i + 1) - primes.get(left - 1) - 2); } else if ((current ^ (current & target)) > 0) { left = i + 1; Arrays.fill(counts, 0); } } System.out.println(ans); } static int getMask(int[] counts) { int mask = 0; for (int i = 0; i < counts.length; i++) { if (counts[i] > 0) { mask += (1 << i); } } return mask; } static void addCount(int[] counts, int value) { while (value > 0) { counts[value % 10]++; value /= 10; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }