#!/usr/bin/env python3 import operator from collections import defaultdict from functools import lru_cache, reduce from itertools import dropwhile, takewhile def sqrt_floor(n): l = 5 * n r = 5 sqrt = 0 while l > r: sqrt += 1 l = l - r r += 10 return sqrt def sieve_of_eratosthenes(max): is_prime = [True] * (max + 1) is_prime[0] = False is_prime[1] = False for i in range(2, sqrt_floor(max) + 1): if not is_prime[i]: continue for j in range(i * i, max + 1, i): is_prime[j] = False return filter(lambda x: is_prime[x], range(max + 1)) def set_of_nums(n): return frozenset(list(str(n))) PRIME = [1] + list(sieve_of_eratosthenes(5000000)) PRIME_NUMS = tuple(map(set_of_nums, PRIME)) N = int(input()) A = frozenset(input().split()) maxdistance = -1 count = 0 now = set() from_one = True for i in range(len(PRIME_NUMS)): if PRIME_NUMS[i] <= A: now.update(PRIME_NUMS[i]) count += 1 continue if now == A: maxdistance = max(maxdistance, PRIME[i] - PRIME[i - count - 1] - 2) count = 0 now = set() from_one = False if now == A: if from_one: maxdistance = max(maxdistance, 5000000 - 1) else: maxdistance = max(maxdistance, 5000000 - PRIME[i - count - 1] - 2) print(maxdistance)