// Задача "Баланс"
// 6-ая Интернет-олимпиада сезона 2008-2009, базовый уровень
// Автор задачи: Владимир Ульянцев, ulyantsev@rain.ifmo.ru
// Автор решения: Антон Банных, argentony@gmail.com

import java.io.*;
import java.math.*;
import java.util.*;

public class balance_ab implements Runnable {
	public static void main(String[] args) {
		new Thread(new balance_ab()).start();
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;

	public void run() {
		try {
			br = new BufferedReader(new FileReader("balance.in"));
			out = new PrintWriter("balance.out");
			solve();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}

	String nextToken() {
		while (st == null || !st.hasMoreElements()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				throw new Error("Enexpected end of file");
			}
		}
		return st.nextToken();
	}

	void myAssert(boolean e, String msg) {
		if (!e) {
			throw new Error(msg);
		}
	}

	int nextInt() {
		return Integer.parseInt(nextToken());
	}

	int nextInt(int l, String name, int r) {
		int ans = nextInt();
		myAssert(l <= ans && ans <= r, name + " out of bounds exception");
		return ans;
	}
	
	long nextLong() {
		return Long.parseLong(nextToken());
	}

	double nextDouble() {
		return Double.parseDouble(nextToken());
	}

	int MAX_N = 1000;
	int MAX_A = 1000000;
	
	void solve() throws IOException {
		int n = nextInt(1, "n", MAX_N);
		long[] a = new long[n];
		long sum = 0;
		for (int i = 0; i < n; ++i) sum = sum + (a[i] = nextInt(0, "a[" + i + "]", MAX_A));
		if (sum % n != 0) {
			out.println(-1);
			return;
		}
		long av = sum / n;
		for (int i = 0; i < n; ++i) a[i] -= av;
		
		long ans = Long.MAX_VALUE;
		for (int i = 0; i < n; ++i) {
			long tans = 0;
			long fl = 0;
			for (int j = 0; j < n; ++j) {
				tans += Math.abs(fl);
				fl += a[(i + j) % n];
			}
			ans = Math.min(ans, tans);
		}
		out.println(ans);
	}
	
	long INF = Long.MAX_VALUE / 2;
}
