import random import itertools def is_prime(q,k=5): q=abs(q) if q==2 or q==3:return True if q==1:return False if q%2==0 or q%3==0:return False d=(q-1)>>1 while d&1==0: d>>=1 for i in range(k): a=random.randint(1,q-1) t=d y=pow(a,t,q) while t!=q-1 and y!=1 and y!=q-1: y=pow(y,2,q) t<<=1 if y!=q-1 and t&1==0: return False return True n=int(input()) a=list(map(int,input().split())) s=[] for i in list(itertools.permutations(a)): s.append(int(''.join(map(str,i)))) s.sort(reverse=True) p=0 for i in s: if p-i: if is_prime(i): print(i) break p=i else: print(-1)