import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int[] A = new int[10]; for(int i = 0; i < n; i++) { A[a[i]] = 1; } int ans = -1; ArrayList prime = new ArrayList(); boolean[] is_prime = new boolean[5000001]; for(int i = 0; i <= 5000000; i++) is_prime[i] = true; is_prime[0] = false; is_prime[1] = false; for(int i = 2; i <= 5000000; i++) { if(is_prime[i]) { prime.add((int)i); for(int j = 2 * i; j <= 5000000; j += i) is_prime[j] = false; } } int[] P = new int[5000001]; for(int i = 0; i < prime.size(); i++) { P[prime.get(i)] = 1; } int p = 1; for(int i = 1; i <= 5000000; i++) { int[] keta = new int[10]; int t = 0; for(int j = p; j <= 5000000; j++) { if(P[j] == 1) { String str = String.valueOf(j); for(int k = 0; k < str.length(); k++) { int sk = Integer.parseInt(String.valueOf(str.charAt(k))); keta[sk]++; } int c = 2; for(int k = 0; k < 10; k++) { if(A[k] == 1) { if(keta[k] == 0) c = Math.min(c, 1); } else { if(keta[k] == 0) c = Math.min(c, -1); } } if(c == 2) { ans = Math.max(ans, j - i + 1); if(j == 5000000) { p = 5000001; break; } } else if(c == 1) { if(j == 5000000) { p = 5000001; break; } } else { p = Math.max(i + 1, j); break; } } } } System.out.println(ans); } }