#include <bits/stdc++.h>
using namespace std;
const long double pi = acos(-1);
long long a[1000002] = {};
long long b[200002] = {};
string s = "", ss = "";
long long n, m, k, w;
long long ans, ans1;
long long mod = 1e9 + 7;
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int t = 1;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    ;
    for (long long i = 0; i < n; i++) m += a[i];
    if (m == 1) return cout << -1, 0;
    vector<long long> d;
    for (long long i = 2; i * i <= m; i++) {
      if (m % i == 0) {
        d.push_back(i);
        while (m % i == 0) m /= i;
      }
    }
    if (m > 1) d.push_back(m);
    for (long long i = 1; i < n; i++) a[i] += a[i - 1];
    ans = 1e18;
    for (long long j = 0; j < d.size(); j++) {
      k = 0;
      for (long long i = 0; i < n - 1; i++)
        k += min(a[i] % d[j], d[j] - (a[i] % d[j]));
      ans = min(ans, k);
    }
    cout << ans;
  }
  return 0;
}
