def SieveofEratosthenes(N): P = [0] * (N+1) P[0] = P[1] = -1 for i in range(2,N+1): if P[i] == 0: k = i while k <= N: P[k] = i k += i P[i] = 0 return P from collections import defaultdict M = 5000000 P = SieveofEratosthenes(M) N = int(input()) A = list(map(int,input().split())) B = [] for i in range(10): if not i in A: B.append(i) C = [] for i in range(2,M+1): if P[i] == 0: C.append(i) M = len(C) S = [[0] * (10) for _ in range(M+1)] for i in range(M): x = C[i] if x == 0 or x == 1 or x == M+1: continue while x: S[i+1][x%10] += 1 x //= 10 Left = 0 dic = defaultdict(int) ans = -1 for Right in range(M): for i in range(10): dic[i] += S[Right][i] while Left <= Right: flg = True for i in B: if dic[i]: flg = False if flg: break Left += 1 for i in range(10): dic[i] -= S[Left][i] flg = True for i in A: if not dic[i]: flg = False if flg: if C[Left] == 2: L = 1 else: L = C[Left-1] + 1 if C[Right] == 4999999: R = 5000000 else: R = C[Right] - 1 ans = max(ans, R - L) print(ans)