#pragma GCC optimize("O3") #pragma GCC target("tune=native") #pragma GCC target("avx") #include using ll = long long; std::mt19937 mt{std::random_device{}()}; ll mul(const ll n, const ll m, const ll mod) { return m == 0 ? 0 : m % 2 == 1 ? (mul(n, m - 1, mod) + n) % mod : mul(2 * n % mod, m / 2, mod); } ll power(const ll a, const ll n, const ll mod) { return n == 0 ? 1 : n % 2 == 1 ? mul(power(a, n - 1, mod), a, mod) : power(mul(a, a, mod), n / 2, mod); } bool MillerRabin(const ll n, const int rep = 10) { if (n == 1) return false; ll s = 0, d = n - 1; for (; d % 2 == 0; s++, d /= 2) {} std::uniform_int_distribution dist{1, n - 1}; for (int i = 0; i < rep; i++) { const ll a = dist(mt); if (power(a, n - 1, n) != 1) { return false; } ll num = power(a, d, n); if (num == 1) { continue; } bool comp = true; for (ll r = 0; r < s; r++) { if (num == n - 1) { comp = false; break; } num = mul(num, num, n); } if (comp) { return false; } } return true; } int main() { int N; std::cin >> N; std::vector A(N); for (int i = 0; i < N; i++) { std::cin >> A[i]; } ll ans = -1; do { std::string s; for (const int e : A) { s += std::to_string(e); } const ll p = std::stoll(s); if (MillerRabin(p)) { ans = std::max(ans, p); } } while (std::next_permutation(A.begin(), A.end())); std::cout << ans << std::endl; return 0; }