#!/usr/bin/python2 # -*- coding: utf-8 -*- # † from itertools import permutations from random import randrange def memoize(func): cache = {} def _memoizer(*args): res = cache.get(args) if res == None: res = cache[args] = func(*args) return res _memoizer.cache = cache return _memoizer @memoize def is_probable_prime(n, k=20): if n == 1: return False for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]: if n == p: return True if n % p == 0: return False s, d = 0, n-1 while d & 1 == 0: s, d = s+1, d>>1 for _ in xrange(k): a = randrange(2, n-1) x = pow(a, d, n) if x == 1 or x == n-1: continue for _ in xrange(s-1): x = x * x % n if x == n-1: break # could be strong liar, try another a else: return False # composite if we reached end of this loop return True # probably prime if reached end of outer loop n = int(raw_input()) v = map(int, raw_input().split()) maxi = -1 for perm in permutations(v): x = int(''.join(map(str, perm))) if maxi < x and is_probable_prime(x): maxi = x print maxi