#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import itertools _, *A = map(int, read().split()) A_str = [str(x) for x in A] def gen_nums(): for p in itertools.permutations(A_str): yield int(''.join(p)) def MillerRabinTest(n, maxiter=20): import random if n == 1: return False elif n == 2: return True if not n & 1: return False d = n - 1 while not (d & 1): d >>= 1 for _ in range(maxiter): a = random.randint(1, n - 1) t = d x = pow(a, t, n) while (t != n - 1) and (x != 1) and (x != n - 1): x = x * x % n t <<= 1 if (x != n - 1) and not (t & 1): return False return True answer = -1 for p in gen_nums(): if answer < p and MillerRabinTest(p): answer = p print(answer)