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[5 * MAX + 1]; Arrays.fill(is_prime, true); is_prime[0] = is_prime[1] = false; for(int i = 2; i <= 5 * MAX; i++){ if(!is_prime[i]){ continue; } prime_tree.add(i); for(int j = i * 2; j <= 5 * MAX; j += i){ is_prime[j] = false; } } //System.out.println(valid_set); if(valid_set.size() == 10){ System.out.println(4999999); return; } int lower = 1, upper = 1; // [lower, upper] int[] count = new int[10]; int max = Integer.MIN_VALUE; while(lower <= upper){ if(valid_range(count, valid_set) && upper < MAX){ final int lower_floor = (is_prime[lower] ? lower - 1 : prime_tree.floor(lower - 1) + 1); final int upper_ceiling = (is_prime[upper] ? prime_tree.ceiling(upper + 1) : prime_tree.ceiling(upper)) - 1; //System.out.println(lower + " (" + lower_floor + " , " + upper_ceiling + ") [" + (upper_ceiling - lower_floor + 1) + "] " + upper); max = Math.max(max, upper_ceiling - lower_floor); //System.out.println(max); upper++; if(is_prime[upper]){ update(count, Integer.toString(upper), true); } }else if(!valid_range(count, valid_set) && 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); } }