import java.math.BigInteger;

public class GoogleQ4 {

	public static Rule[] r;

	public static void main(String[] args) {
		Prime.cty = Integer.parseInt(args[0]); // pull the probable primality certainty from the arguments aswell. the first argument.
		
		r = new Rule[args.length - 1]; // the rest of the args are parameters for the question.
		for (int i = 1; i < args.length; i++)
			r[i - 1] = new Rule(Integer.parseInt(args[i]));
		
		long start = new java.util.Date().getTime();
		BigInteger out = match(0);
		System.out.println("*****************************************************************************");
		System.out.println(out + "    -> computed in " + ((new java.util.Date().getTime() - start) / 1000) + " seconds.");
		System.out.println("*****************************************************************************");
	}
	
	public static BigInteger match(int lev) {
		r[lev].pop(new BigInteger("2"));
		if (lev == 0) {
			boolean flag = false;
			while (!flag) {
				if (Prime.isPrime(r[0].total)) { // only check sub rules if this number is prime, else move on
					flag = (r[0].total.equals(match(1)));
					if (flag) return r[0].total;
				}
				r[lev].shift(); // move this rule on,... then recursively check sub rules again.
			}
		} else {
			// The following two loops are where I'm guessing most of our CPU time is dissapearing too. These can be optimised.
			r[lev].pop(r[lev - 1].parts[r[lev - 1].target - 1]); // before we manually loop to find matches we must set a starting position. Here I'm choosing to start from the end of the previous rule.
			for (int p = 0; r[lev].total.subtract(r[lev - 1].total).longValue() > 0; p++)
				r[lev].back(); // carry on reversing this rules total until it's members accumulate to less than the previous rules total.
			// now go back slowly
			while (r[lev].total.subtract(r[lev - 1].total).longValue() < 0)
				r[lev].shift(); // carry on shifting this rules total sideways until it's members accumulate to the previous rules total.
			
			if (r[lev].total.equals(r[lev - 1].total)) { // hoorah, one step forward
				if (lev == r.length - 1) { // hoorah^2. found it.
					return r[lev].total;
				} else { // more rules need to be verified..
					return match(lev + 1);
				}
			} else { // damn,.. one step back.
				return r[lev].total;
			}
		}
		return new BigInteger("0");
	}

}

class Rule {

	int target;
	BigInteger[] parts;
	BigInteger total;

	public Rule(int target) {
		this.target = target;
		parts = new BigInteger[target];
	}
	
	public void pop(BigInteger seed) {
		parts[0] = seed;
		total = parts[0];
		for (int j = 1; j < target; j++) {
			parts[j] = new BigInteger("" + Prime.next(parts[j-1]));
			total = total.add(parts[j]);
		}
	}
	
	public BigInteger shift() {
		total = total.subtract(parts[0]);
		for (int j = 1; j < target; j++)
			parts[j-1] = parts[j];
		parts[target-1] = new BigInteger("" + Prime.next(parts[target-2]));
		total = total.add(parts[target-1]);
		return total;
	}
	
	public BigInteger back() {
		total = total.subtract(parts[target-1]);
		for (int j = 1; j < target; j++)
			parts[target-j] = parts[target-j-1];
		parts[0] = new BigInteger("" + Prime.prev(parts[1]));
		total = total.add(parts[0]);
		return total;
	}
	
}

class Prime {
// This class is begging for optimisation.

	public static int cty = 20;

	public static boolean isPrime(BigInteger c) {
		return c.isProbablePrime(cty);
	}

	public static BigInteger next(BigInteger n) {
		BigInteger possiblePrime = new BigInteger("" + ((n.mod(new BigInteger("2")).equals(new BigInteger("0")) ? n.add(new BigInteger("1")) : n.add(new BigInteger("2")))));
		while(true) {
			if(isPrime(possiblePrime)) return possiblePrime;
			possiblePrime = possiblePrime.add(new BigInteger("2"));
		}
	}	
	
	public static BigInteger prev(BigInteger n) {
		BigInteger possiblePrime = new BigInteger("" + ((n.mod(new BigInteger("2")).equals(new BigInteger("0")) ? n.subtract(new BigInteger("1")) : n.subtract(new BigInteger("2")))));
		while(true) {
			if(isPrime(possiblePrime)) return possiblePrime;
			possiblePrime = possiblePrime.subtract(new BigInteger("2"));
		}
	}	
	
}
