// 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, K; ll ans = -1, pre = -1; void showperm(char *p) { int i; long long x; x = 0; for (i = 0; i < n; i++) { x *= 10; if (a[p[i]] >= 10) x *= 10; x += a[p[i]]; } if (x == pre) return; pre = x; if (miller_rabin(x) && x > ans) ans = x; } // Kの数{ 1,2,3,4,5,6,7,8,9 } の順列を生成する K!個 void genperm() { int k, t; char c[12], *pc, *q; static char p[12] = { 1,1,1,1,1,1,1,1,1 }; q = p, pc = c; for (k = 1; k <= K; ) *q++ = *pc++ = k++; // 1 2 3 のように1から k = 1, pc = c; do { t = *(p + k); *(p + k) = *(q = p + ((k & 1) ? *pc : 0)); *q = t; showperm(p); k = 1, pc = c; while (*pc == 0) *pc++ = k++; (*pc)--; } while (k < K); } int main() { int i; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", a+i); K = n; genperm(); printf("%lld\n", ans); return 0; }