#include #include using namespace std; long long powMod(long long x, long long k, long long m) { if (k == 0) return 1; if (k % 2 == 0) return powMod(x * x % m, k / 2, m); else return x * powMod(x, k - 1, m) % m; } bool suspect(long long a, int s, long long d, long long n) { long long x = powMod(a, d, n); if (x == 1) return true; for (int r = 0; r < s; ++r) { if (x == n - 1) return true; x = x * x % n; } return false; } // {2,7,61,-1} is for n < 4759123141 (= 2^32) // {2,3,5,7,11,13,17,19,23,-1} is for n < 10^16 (at least) bool isPrime(long long n) { if (n <= 1 || (n > 2 && n % 2 == 0)) return false; int test[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, -1}; long long d = n - 1, s = 0; while (d % 2 == 0) ++s, d /= 2; for (int i = 0; test[i] < n && test[i] != -1; ++i) if (!suspect(test[i], s, d, n)) return false; return true; } int main() { int n; cin >> n; vector a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); long long ans = 0; do { string tmp; for (int i = 0; i < n; i++) tmp += a[i]; long long check = stoll(tmp); cerr << check << endl; if (isPrime(check)) ans = max(ans, check); } while (next_permutation(a.begin(), a.end())); cout << (ans == 0 ? -1 : ans) << endl; return 0; }