#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
const int lim = 2 * 3 * 5 * 7 * 11 * 13 * 17; // = 510510
vector<int> primes;
bool isprime(long long x) {
	if (x == 1) return false;
	if (x == 2) return true;
	if (x % 2 == 0) return false;
	if (x == 3) return true;
	if (x % 3 == 0) return false;
	if (x == 5) return true;
	if (x % 5 == 0) return false;
	if (x == 7) return true;
	if (x % 7 == 0) return false;
	if (x == 11) return true;
	if (x % 11 == 0) return false;
	if (x == 13) return true;
	if (x % 13 == 0) return false;
	if (x == 17) return true;
	if (x % 17 == 0) return false;
	bool finish = false;
	for (int i = 0; !finish; i += lim) {
		for (int j : primes) {
			long long t = i + j;
			if (t * t > x) {
				finish = true;
				break;
			}
			if (x % t == 0) return false;
		}
	}
	return true;
}
void initialize() {
	for (int i = 2; i <= lim + 1; ++i) {
		if (i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0 && i % 11 != 0 && i % 13 != 0 && i % 17 != 0) {
			primes.push_back(i);
		}
	}
}
int main() {
	initialize();
	int N;
	cin >> N;
	vector<int> A(N);
	int sum = 0;
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
		sum += A[i];
	}
	if (sum % 3 == 0) {
		if (N == 1 && sum == 3) cout << 3 << endl;
		else cout << -1 << endl;
	}
	else {
		vector<long long> S;
		vector<int> perm(N);
		for (int i = 0; i < N; ++i) {
			perm[i] = A[i];
		}
		sort(perm.begin(), perm.end());
		do {
			string str;
			for (int i = 0; i < N; ++i) {
				str += to_string(perm[i]);
			}
			S.push_back(stoll(str));
		} while (next_permutation(perm.begin(), perm.end()));
		sort(S.begin(), S.end(), greater<long long>());
		S.erase(unique(S.begin(), S.end()), S.end());
		long long ans = -1;
		for (long long i : S) {
			if (isprime(i)) {
				ans = i;
				break;
			}
		}
		cout << ans << endl;
	}
	return 0;
}