#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
long long arr[10000000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long total;
  cin >> total;
  long long sum = 0;
  for (long long i = 1; i <= total; i++) {
    long long val;
    cin >> val;
    arr[i] = val;
    sum += val;
  }
  long long ans = 399999999999005000;
  for (long long i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      while (sum % i == 0) sum /= i;
      v.push_back(i);
    }
  }
  if (sum != 1) v.push_back(sum);
  for (long long i = 0; i < v.size(); i++) {
    long long u = v[i];
    long long sum = 0;
    long long temp = 0;
    for (long long j = 1; j <= total; j++) {
      temp += arr[j];
      temp %= u;
      sum += min(temp, u - temp);
    }
    ans = min(ans, sum);
  }
  if (ans != 399999999999005000)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
