// Arup Guha
// 11/5/2013
// Solution to 2013 South East Regional Problem A: Mountains

// Note: This takes 20 seconds on my laptop on the judge data, which I think is entirely reasonable.
//       Asymptotically, this is the same speed as the judge solution. The run time is O(mn),
//       where n is the length of the input sequence and m is the number of prime sequences (a bit under 5000).
//       I might be able to cut out of some searches earlier, but that would ruin the simplicity of this solution.

import java.util.*;

public class mountains {

	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) {

		sieve = new boolean[LIMIT];
		Arrays.fill(sieve, true);
		sieve[0] = false;
		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;

		// Generate all sequences.
		seq = new int[LIMIT];
		allSeq = new int[MAXSEQ][4];
		allSeqLen = new int[MAXSEQ];
		generate();

		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=1; i<seqCnt; i++) {
				long tmp = solve(i);
				if (tmp < best)
					best = tmp;
			}

			// Output solution and move on!
			System.out.println(best);
			n = stdin.nextInt();
		}
	}

	public static long solve(int listNo) {

		// Set initial value.
		int size = allSeqLen[listNo];
		int[] pos = Arrays.copyOf(allSeq[listNo], size);

		// This is big enough, right?
		long best = 1000000000000000000L;

		// Shift our positions...
		while (pos[size-1] < n) {

			// Evaluate current, update best if necessary.
			long cur = eval(pos, size);
			if (cur < best) best = cur;

			// Go to next in list.
			for (int i=0; i<size; i++) pos[i]++;
		}

		return best;
	}

	// Evaluate moving all the blocks to the positions listed in pos.
	public static long eval(int[] pos, int size) {

		long sum = 0;
		int saveright = 0;

		// Count up sum to move items in range to pos[i].
		for (int i=0; i<size; i++) {

			int left, right;

			// Set left and right, watching out for ends of list.
			if (i == 0) left = 0;
			else left = saveright+1;

			if (i == size-1) right = n-1;
			else right = (pos[i]+pos[i+1])/2;
			saveright = right;

			// Add left side.
			sum +=  ( (weightRevF[left]-weightRevF[pos[i]]) - (cumRevF[left] - cumRevF[pos[i]])*(n-1-pos[i]) );

			// Add right side.
			sum +=  ( (weightF[right+1]-weightF[pos[i]+1]) - (cumF[right+1] - cumF[pos[i]+1])*pos[i] );
		}

		return sum;
	}

	// Generates all prime sequences described in the problem - wrapper function.
	public static void generate() {
		seq[0] = 0;
		top = 1;
		genRec();
	}

	// Recursively generates all prime sequences in the problem.
	public static void genRec() {

		int last = seq[top-1];
		for (int i=last+2; i<30000; i++) {
			if (okay(i)) {
				seq[top++] = i;
				genRec();
				top--;
			}
		}

		// Store sequence, length and advance sequence count.
		for (int i=0; i<top; i++)
			allSeq[seqCnt][i] = seq[i];
		allSeqLen[seqCnt] = top;
		seqCnt++;
	}

	// Checks if it's valid to add next to a sequence that exists.
	public static boolean okay(int next) {
		for (int i=0; i<top; i++)
			if (!sieve[next-seq[i]])
				return false;
		return true;
	}
}