// yukicoder 774 tatyamと素数大富豪 // 2019.5.12 bal4u #include #include typedef long long ll; typedef unsigned long long ull; //// ラビン素数テスト #define mulmod128(a,b,n) ((__int128_t)a*b % n) ull powmod(ull a, ull k, ull n) { ull bit = 0x2000000000000000LL, p = 1; while (bit) { if (p > 1) p = mulmod128(p, p, n); if (k & bit) p = mulmod128(p, a, n); bit >>= 1; } return p; } unsigned xorshift(int id) { static unsigned y = 2463534242; y = y ^ (y << 13), y = y ^ (y >> 17), y = y ^ (y << 5); return y; } ull gcd(ull a, ull b) { ull r; while (b != 0) r = a % b, a = b, b = r; return a; } int suspect(ull n) { int i, j, b; ull t, u, x; u = n-1, t = 0; while ((u & 1) == 0) u >>= 1, t++; for (j = 0; j < 6; j++) { do b = xorshift(j) % n; while (gcd(b, n) > 1); x = powmod(b, u, n); if (x == 1 || x == n-1) continue; for (i = 1; i < t; i++) { x = mulmod128(x, x, n); if (x == 1) return 0; if (x == n-1) break; } if (i == t) return 0; } return 1; } int miller_rabin(ull n) { if (n <= 1) return 0; if (n == 2 || n == 3) return 1; if ((n & 1) == 0) return 0; return suspect(n); } int a[15], n; int t[15], f[15]; ll ans = -1; void rec(int id) { int i, pre; long long x; if (id == n) { x = 0; for (i = 0; i < n; i++) { x *= 10; if (t[i] >= 10) x *= 10; x += t[i]; } if (miller_rabin(x) && x > ans) ans = x; return; } pre = -1; for (i = 0; i < n; i++) { if (f[i] || a[i] == pre) continue; f[i] = 1, t[id] = pre = a[i]; rec(id+1); f[i] = 0; } } int main() { int i; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", a+i); rec(0); printf("%lld\n", ans); return 0; }