from collections import Counter N=int(input()) A=Counter(input().split()) x=5000000 import math L=math.floor(math.sqrt(x)) # 平方根を求める Primelist=[i for i in range(x+1)] Primelist[1]=0 # 1は素数でないので0にする. for i in Primelist: if i>L: break if i==0: continue for j in range(2*i,x+1,i): Primelist[j]=0 Primes=[0]+[Primelist[j] for j in range(x+1) if Primelist[j]!=0] P2=[] for p in Primes: P2.append(Counter(str(p))) S=Counter() ANS=-1 end=1 for now in range(1,len(Primes)): while set(S)<=set(A): if set(S)==set(A): if end==348514: ANS=max(ANS,5000001-Primes[now-1]-2) else: ANS=max(ANS,Primes[end]-Primes[now-1]-2) if end==348514: break S+=P2[end] end+=1 #print(Primes[now],Primes[end],ANS,A,S) S-=P2[now] print(ANS)