#include <bits/stdc++.h>
using namespace std;
const long long N = 2000010;
long long n, a, s[N];
bool isprime[N];
vector<long long> prime;
void seive() {
  isprime[0] = 1;
  isprime[1] = 1;
  for (long long i = 4; i <= N / 2; i += 2) isprime[i] = 1;
  for (long long i = 3; i * i <= N / 2; i += 2) {
    if (isprime[i] == 0)
      for (long long j = i * i; j <= N / 2; j += 2 * i) isprime[j] = 1;
  }
  for (long long i = 2; i <= N / 2; i++)
    if (isprime[i] == 0) prime.push_back(i);
}
vector<long long> pf;
void primefactor(long long num) {
  seive();
  for (long long i = 0; prime[i] * prime[i] <= num; i++) {
    if (num % prime[i] == 0) {
      pf.push_back(prime[i]);
      while (num % prime[i] == 0) {
        num /= prime[i];
      }
    }
  }
  if (num != 1) {
    pf.push_back(num);
  }
  return;
}
int main() {
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a);
    if (i == 0)
      s[i] = a;
    else
      s[i] += s[i - 1] + a;
  }
  primefactor(s[n - 1]);
  long long res = 1e18;
  for (auto k : pf) {
    long long x = 0;
    for (int i = 0; i < n - 1; i++) {
      x += min(s[i] % k, k - (s[i] % k));
    }
    res = min(x, res);
  }
  if (res == 1e18) res = -1;
  cout << res << endl;
  return 0;
}
