#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
long long n, a[N], sum;
vector<long long> v;
void fact() {
  if (sum % 2 == 0) {
    v.push_back(2);
    while (sum % 2 == 0) sum /= 2;
  }
  for (long long i = 3; i * i <= sum; i += 2) {
    if (sum % i == 0) {
      v.push_back(i);
      while (sum % i == 0) sum /= i;
    }
  }
  if (sum > 2) v.push_back(sum);
}
long long solve(long long p) {
  long long tmp, tmp2, res = 0;
  for (int i = 1; i < n; i++) {
    tmp = a[i] % p, tmp2 = p - tmp;
    if (tmp < tmp2)
      res += tmp;
    else
      res += tmp2;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
    a[i] = a[i - 1] + a[i];
  }
  fact();
  if (!v.size()) return cout << "-1\n", 0;
  long long ans = 1e18;
  for (int i = 0; i < v.size(); i++) {
    long long p = v[i];
    ans = min(ans, solve(p));
  }
  cout << ans << "\n";
  return 0;
}
