def mark(s, x): for i in range(x + x, len(s), x): s[i] = False def sieve(n): s = [True] * n for x in range(2, int(n**0.5) + 1): if s[x]: mark(s, x) return [i for i in range(n) if s[i] and i > 1] def solve(A): MAX = 5000000 P = sieve(MAX) B = sum(1 << a for a in A) b2 = {str(c): 1 << c for c in range(10)} M = [sum(b2[c] for c in set(str(p))) for p in P] l = r = ps = s = 0 ans = -1 while r < len(M): sb = s & B if s - sb: if ps == B: if r == len(M) - 1: r1 = MAX else: r1 = P[r - 1] - 1 if l == 0: l1 = 1 else: l1 = P[l - 1] + 1 ans = max(ans, r1 - l1) l = r s = 0 else: ps = s s = ps | M[r] r += 1 if ps and ans == -1: return 4999999 return ans def main(): input() A = list(map(int, input().split())) print(solve(A)) if __name__ == '__main__': main()