// Arup Guha
// 11/5/2013
// Solution to 2013 South East Regional Problem A: Mountains


// Note: This takes 11 seconds on my laptop and uses an optimization suggested by Antony.
//       Basically, the optimization strips 6 seconds off the run-time. It's a slight
//       change in algorithm since I only consider sequences of length 2 (denoted by the
//       prime difference, and then add one spot to the left or right with distance 2
//       from the left or right end. This automatically builds all sequences of consequence
//       without explicitly building them like I did in my old solution. To account for this
//       I have to calculate in O(1) time the reduction in cost for adding those spots. This
//       was the key change in algorithm between the old solution and this one. My motivation
//       in pursuing this is that my old solution didn't pass on Glenn's autojudge time limit.
//       I suspect my old solution would have passed the SER run time.


import java.util.*;

public class mountains2 {

	final public static int LIMIT = 30000;
	final public static int MAXSEQ = 5000;

	// Used to store sequences of prime gaps that fit the pattern.
	public static boolean[] sieve;
	public static int[] seq;
	public static int top;
	public static int[][] allSeq;
	public static int[] allSeqLen;
	public static int seqCnt = 0;

	// Stores input and look up tables related to input.
	public static int[] input;
	public static long[] cumF;
	public static long[] weightF;
	public static long[] cumRevF;
	public static long[] weightRevF;

	public static int n;

	public static void main(String[] args) {

//		long start = System.currentTimeMillis(), end;

		sieve = new boolean[LIMIT];
		Arrays.fill(sieve, true);
		sieve[0] = true; // So we can check a single spot.
		sieve[1] = false;

		// Run prime sieve.
		for (int i=2; i<LIMIT; i++)
			for (int j=2*i; j<LIMIT; j+=i)
				sieve[j] = false;

		Scanner stdin = new Scanner(System.in);
		n = stdin.nextInt();

		// Go through each case.
		while (n != 0) {

			// Store the input.
			input = new int[n];
			for (int i=0; i<n; i++)
				input[i] = stdin.nextInt();

			// Look up table to get sum of any contiguous subsequence in O(1) time.
			cumF = new long[n+1];
			weightF = new long[n+1];
			cumF[0] = 0;
			weightF = new long[n+1];
			for (int i=1; i<=n; i++) {
				cumF[i] = cumF[i-1] + input[i-1];
				weightF[i] = weightF[i-1] + input[i-1]*(i-1);
			}

			// Look up table from the left.
			cumRevF = new long[n+1];
			weightRevF = new long[n+1];
			cumRevF[n] = 0;
			weightRevF[n] = 0;
			for (int i=n-1; i>=0; i--) {
				cumRevF[i] = cumRevF[i+1] + input[i];
				weightRevF[i] = weightRevF[i+1] + input[i]*(n-1-i);
			}

			// Find best match for each fixed prime sequence and update our overall best as needed.
			long best = solve(0);
			for (int i=2; i<LIMIT; i++) {
				if (sieve[i]) {
					long tmp = solve(i);
					if (tmp < best) {
						best = tmp;
					}
				}
			}

			// Output solution and move on!
			System.out.println(best);
			n = stdin.nextInt();
		}
//		end = System.currentTimeMillis();
//		System.out.println("ms spent = "+(end-start));

	}

	public static long solve(int prime) {

		// This is big enough, right?
		long best = 1000000000000000000L;

		// Shift our positions...
		int offset = 0;
		while (prime+offset < n) {

			// Evaluate current, update best if necessary.
			long cur = eval(prime, offset);
			if (cur < best) best = cur;

			// Go to next in list.
			offset++;
		}

		return best;
	}

	// Evaluate moving all the blocks to the positions listed in pos.
	public static long eval(int prime, int offset) {

		int i1 = offset;
		int i2 = prime+offset;

		long sum = 0;
		int saveright = 0;

		int left = 0, right = (i1+i2)/2;
		if (i1 == i2) right = n-1;

		// Add left side.
		sum +=  ( (weightRevF[left]-weightRevF[i1]) - (cumRevF[left] - cumRevF[i1])*(n-1-i1) );
		sum +=  ( (weightF[right+1]-weightF[i1+1]) - (cumF[right+1] - cumF[i1+1])*i1 );

		if (i1 == i2) return sum;

		left = right+1;
		right = n-1;

		// Add left side.
		sum +=  ( (weightRevF[left]-weightRevF[i2]) - (cumRevF[left] - cumRevF[i2])*(n-1-i2) );
		sum +=  ( (weightF[right+1]-weightF[i2+1]) - (cumF[right+1] - cumF[i2+1])*i2 );

		if (!sieve[prime+2]) return sum;

		long lDiscount = 0, rDiscount = 0;
		if (offset >= 2)
			lDiscount = cumF[offset-1]*2;
		if (offset+prime+2 <= n)
			rDiscount = cumRevF[prime+offset+2]*2;

		if (prime == 3) return sum - lDiscount - rDiscount;
		else            return sum - Math.max(lDiscount, rDiscount);

	}

}