using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using System.Text.RegularExpressions; using System.Linq; using System.IO; class Program { static void Main(string[] args) { new Magatro().Solve(); } } public class Scanner { private StreamReader Sr; private string[] S; private int Index; private const char Separator = ' '; public Scanner() { Index = 0; S = new string[0]; Sr = new StreamReader(Console.OpenStandardInput()); } private string[] Line() { return Sr.ReadLine().Split(Separator); } public string Next() { string result; if (Index >= S.Length) { S = Line(); Index = 0; } result = S[Index]; Index++; return result; } public int NextInt() { return int.Parse(Next()); } public double NextDouble() { return double.Parse(Next()); } public long NextLong() { return long.Parse(Next()); } public decimal NextDecimal() { return decimal.Parse(Next()); } public string[] StringArray(int index = 0) { Next(); Index = S.Length; return S.Skip(index).ToArray(); } public int[] IntArray(int index = 0) { return StringArray(index).Select(int.Parse).ToArray(); } public long[] LongArray(int index = 0) { return StringArray(index).Select(long.Parse).ToArray(); } public bool EndOfStream { get { return Sr.EndOfStream; } } } public class Magatro { private Scanner Cin; private int N; private bool[] A; private int[] Primes; const int Max = 5000000; bool[] check; private void Scan() { Cin = new Scanner(); N = Cin.NextInt(); A = new bool[10]; for (int i = 0; i < N; i++) { A[Cin.NextInt()] = true; } } public void Solve() { Scan(); Primes = SieveOfEratosthenes(Max); check = new bool[10]; int length = -1; bool flag = false; for (int i = 0; i < Primes.Length;) { bool[] temp = UseNum(Primes[i]); bool canUse = true; for (int j = 0; j < 10; j++) { if (temp[j] && !A[j]) canUse = false; } if (!canUse) { i++; continue; } else { bool f = false; // Console.WriteLine(Primes[i]); for (int j = i; j < Primes.Length; j++) { bool[] b = UseNum(Primes[j]); for (int k = 0; k < 10; k++) { check[k] |= b[k]; } bool equal = true; bool bad = false; for (int k = 0; k < 10; k++) { if (check[k] != A[k]) equal = false; if (check[k] && !A[k]) bad = true; } if (equal) { int left; if (i == 0) { left = 1; } else { left = Primes[i - 1] + 1; } int right; if (j == Primes.Length - 1) { right = Max; } else { right = Primes[j + 1] - 1; } length = Math.Max(length, right - left); } if (bad) { i = j + 1; check = new bool[10]; f = true; break; } } if (!f) { break; } } } Console.WriteLine(length); } private bool[] UseNum(int n) { bool[] result = new bool[10]; while (n > 0) { result[n % 10] = true; n /= 10; } return result; } private int[] SieveOfEratosthenes(int max) { bool[] table = new bool[max + 1]; table[0] = table[1] = true; int i = 2; for (int j = i * 2; j <= max; j += 2) { table[j] = true; } int loop = (int)Math.Sqrt(max); for (i = 3; i <= loop; i += 2) { if (!table[i]) { for (int j = i * 2; j < max; j += i) { table[j] = true; } } } List result = new List(); for (int j = 0; j <= max; j++) { if (!table[j]) result.Add(j); } return result.ToArray(); } }