n = int(input()) a = [False] * 10 for x in map(int, input().split()): a[x] = True sieve = [True] * 5000002 ans = -1 k = 1 d = [False] * 10 for p in range(2, 5000002): if sieve[p] or p == 5000001: for i in range(p * p, 5000002, p): sieve[i] = False if p < 5000001: ok = True for x in map(int, str(p)): if not a[x]: ok = False break if not ok or p == 5000001: for i in range(10): if a[i] != d[i]: break else: ans = max(ans, p - 1 - k) k = p + 1 d = [False] * 10 else: for x in map(int, str(p)): d[x] = True print(ans)