// Задача "Равномерное распределение"
// 8-ая интернет-олимпиада сезона 2008-2009, усложненный уровень
// Автор задачи: Владимир Ульянцев, ulyantsev@rain.ifmo.ru
// Автор решения: Роман Колганов, kolganov@rain.ifmo.ru
// Жадное решение
import java.util.*;
import java.io.*;

public class balance_rk_greedy implements Runnable {
	BufferedReader in;

	StringTokenizer st;

	PrintWriter out;

	public static void main(String[] args) {
		new Thread(new balance_rk_greedy()).start();
	}

	public void run() {
		try {
			Locale.setDefault(Locale.US);
			String filename = "balance_rk_greedy".substring(0,
					"balance_rk_greedy".indexOf('_'));
			in = new BufferedReader(new FileReader(filename + ".in"));
			st = new StringTokenizer("");
			out = new PrintWriter(new File(filename + ".out"));
			solve();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

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

	int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	void myAssert(boolean b, String message) {
		if (!b) {
			throw new Error(message);
		}
	}

	void inBounds(long x, long l, long r, String name) {
		myAssert(l <= x && x <= r, name + " is out of bounds: " + x
				+ " is not in [" + l + ", " + r + "]");
	}

	final int MAXN = 1000;
	final int MAXA = 1000000;

	void solve() throws IOException {
		int n = nextInt();
		inBounds(n, 1, MAXN, "n");
		int[] a = new int[n];
		int sum = 0;
		for (int i = 0; i < n; ++i) {
			a[i] = nextInt();
			sum += a[i];
			inBounds(a[i], 0, MAXA, "a_" + i);
		}
		if (sum % n != 0){
			out.println(-1);
		} else {
			sum /= n;
			int res = 0;
			for (int j = 0; j < n; ++j) {
				int i = j;
				while (a[i] > sum) {
					int rem = a[i] - sum;
					res += rem;
					a[i] = sum;
					if (a[(i + n - 1) % n] - a[(i + 1) % n] >= rem) {
						a[(i + 1) % n] += rem;
					} else if (a[(i + n - 1) % n] - a[(i + 1) % n] <= rem) {
						a[(i + n - 1) % n] += rem;
					} else if (a[(i + n - 1) % n] > a[(i + 1) % n]) {
						rem -= a[(i + n - 1) % n] - a[(i + 1) % n]; 
						a[(i + 1) % n] = a[(i + n - 1) % n];
						a[(i + 1) % n] += rem / 2;
						a[(i + n - 1) % n] += rem - rem / 2;
					} else {
						rem -= a[(i + 1) % n] - a[(i + n - 1) % n]; 
						a[(i + n - 1) % n] = a[(i + 1) % n];
						a[(i + 1) % n] += rem / 2;
						a[(i + n - 1) % n] += rem - rem / 2;
					}
					i = (i + n - 1) % n;
				}
			}
			out.println(res);
		}
	}

}