def primes2(limit): ''' returns a list of prime numbers upto limit. source: Rossetta code: Sieve of Eratosthenes http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Odds-only_version_of_the_array_sieve_above ''' if limit < 2: return [] if limit < 3: return [2] lmtbf = (limit - 3) // 2 buf = [True] * (lmtbf + 1) for i in range((int(limit ** 0.5) - 3) // 2 + 1): if buf[i]: p = i + i + 3 s = p * (i + 1) + i buf[s::p] = [False] * ((lmtbf - s) // p + 1) return [2] + [i + i + 3 for i, v in enumerate(buf) if v] def read_data(): N = int(input()) A = list(map(int, input().split())) return N, A def solve(N, A): limit = 5 * 10**6 primes = primes2(limit) NGs = [True] * 10 for a in A: NGs[a] = False OKset = set(A) prev_p = 0 maxspan = -1 currentset = set() for p in primes: if is_safe(p, NGs): update_set(currentset, p) continue elif currentset == OKset: span = p - prev_p - 2 if maxspan < span: maxspan = span currentset = set() prev_p = p p = limit if currentset == OKset: span = p - prev_p - 1 if maxspan < span: maxspan = span return maxspan def is_safe(p, NGs): while p: p, r = divmod(p, 10) if NGs[r]: return False return True def update_set(currentset, p): while p: p, r = divmod(p, 10) currentset.add(r) N, A = read_data() print(solve(N, A))