#!/usr/bin/ python3.8 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) A = map(int, read().split()) full = 0 for x in A: full += (1 << x) def num_to_set(n): x = 0 while n: n, r = divmod(n, 10) x |= (1 << r) return x U = 5 * 10 ** 6 + 1 is_prime = [0] * U is_prime[2] = 1 for n in range(3, U, 2): is_prime[n] = 1 for p in range(3, U, 2): if p * p >= U: break if is_prime[p]: for i in range(p * p, U, p + p): is_prime[i] = 0 ng = [] ok = [] for p in range(U): if is_prime[p]: s = num_to_set(p) if s & (~full): ng.append(p) else: ok.append((p, s)) ng.append(U) ok.append((U, 0)) L = 1 i = 0 answer = -1 for x in ng: R = x - 1 se = 0 while True: p, s = ok[i] if p > R: break se |= s i += 1 if se == full and answer < R - L: answer = R - L L = x + 1 print(answer)