import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Main { public static void update(int[] count, String str, boolean add){ for(final char ch : str.toCharArray()){ count[Character.getNumericValue(ch)] += (add ? 1 : -1); } } public static boolean valid_range(int[] count, Set valid){ for(int i = 0; i < count.length; i++){ if(valid.contains(i) && count[i] <= 0){ return false; } if(!valid.contains(i) && count[i] > 0){ return false; } } return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); Set valid_set = new HashSet(); for(int i = 0; i < N; i++){ valid_set.add(sc.nextInt()); } final int MAX = 5000000; TreeSet prime_tree = new TreeSet(); boolean[] is_prime = new boolean[MAX + 1]; Arrays.fill(is_prime, true); is_prime[0] = is_prime[1] = false; for(int i = 2; i <= MAX; i++){ if(!is_prime[i]){ continue; } prime_tree.add(i); for(int j = i * 2; j <= MAX; j += i){ is_prime[j] = false; } } //System.out.println(valid_set); int lower = 1, upper = 1; // [lower, upper] int[] count = new int[10]; int max = Integer.MIN_VALUE; while(lower <= upper){ //System.out.println("[" + lower + ", " + upper + "] + " + Arrays.toString(count)); if(valid_range(count, valid_set) && upper <= MAX){ max = Math.max(max, (prime_tree.ceiling(upper) - 1) - (prime_tree.floor(lower - 1) + 1)); //System.out.println(max); upper++; if(is_prime[upper]){ update(count, Integer.toString(upper), true); } }else if(lower < MAX){ if(is_prime[lower]){ update(count, Integer.toString(lower), false); } lower++; if(lower > upper){ if(is_prime[lower]){ update(count, Integer.toString(lower), true); } upper = lower; } }else{ break; } } System.out.println(max == Integer.MIN_VALUE ? -1 : max); } }