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 boolean less_range(int[] count, Set valid){ for(int i = 0; i < count.length; i++){ 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[] count = new int[10]; int answer = -1; int prev = 1; for(int start = 1; start <= MAX; start++){ if(is_prime[start]){ update(count, Integer.toString(start), true); } if(valid_range(count, valid_set)){ //System.out.println(prev + " " + start); answer = Math.max(answer, start - prev); }else{ if(is_prime[start] && !less_range(count, valid_set)){ prev = start + 1; Arrays.fill(count, 0); } } } if(prev < MAX){ //System.out.println(prev); answer = Math.max(answer, MAX - prev); } System.out.println(answer); } }