#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-all-loops")
using namespace std;
ifstream in;
ofstream out;
const long long kk = 1000;
const long long ml = kk * kk;
const long long mod = ml * kk + 7;
const long long inf = ml * ml * ml + 7;
long long n, i, j, sum;
vector<long long> mm, pr;
bool viv = false;
vector<long long> erat(long long last) {
  vector<bool> bad;
  vector<long long> r;
  for (long long i = 0; i < last + 10; i++) bad.push_back(true);
  for (long long i = 2; i <= last; i++)
    if (bad[i]) {
      long long j = 2;
      while (j * i <= last) {
        bad[j * i] = false;
        j++;
      }
      r.push_back(i);
    }
  return r;
}
long long mv(vector<pair<long long, long long>> &v, long long kol) {
  long long sum = 0;
  long long mid = -1;
  bool was = false;
  for (int i = 0; i < v.size(); i++) {
    sum += v[i].first;
    if (!was && sum > kol / 2) mid = v[i].second, was = true;
  }
  long long res = 0;
  for (long long i = 0; i < v.size(); i++)
    res += abs(mid - v[i].second) * v[i].first;
  return res;
}
long long solve(long long kol) {
  if (kol == 1) return inf;
  vector<long long> m = mm;
  for (auto &i : m) i %= kol;
  vector<pair<long long, vector<pair<long long, long long>>>> v;
  v.push_back({kol, {}});
  int now = 0;
  for (long long i = 0; i < n; i++) {
    while (m[i]) {
      long long mn = min(v[now].first, m[i]);
      m[i] -= mn;
      v[now].first -= mn;
      v[now].second.push_back({mn, i});
      if (v[now].first == 0) now++, v.push_back({kol, {}});
    }
  }
  while (v.size() > now) v.pop_back();
  long long ans = 0;
  for (auto &l : v) ans += mv(l.second, kol);
  return ans;
}
long long solve2(long long kol) {
  if (kol == 1) return inf;
  vector<long long> m = mm;
  for (auto &i : m) i %= kol;
  vector<long long> where;
  vector<long long> coor, sums;
  where.push_back(-1);
  coor.push_back(0);
  sums.push_back(0);
  int now = 0;
  long long lsum = 0;
  long long ans = 0;
  long long mid = (kol + 1) / 2;
  for (long long i = 0; i < n; i++) {
    while (m[i]) {
      long long mn = min(kol - lsum, m[i]);
      if (where.back() != -1) {
        ans += mn * (i - where.back());
      }
      if (where.back() == -1) coor.back() += mn * i, sums.back() += mn;
      m[i] -= mn;
      lsum += mn;
      if (where.back() == -1 && lsum >= mid) {
        where.back() = i;
        ans += sums.back() * i - (coor.back());
      }
      if (lsum == kol)
        lsum = 0, where.push_back(-1), coor.push_back(0), sums.push_back(0);
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  pr = erat(ml + 10);
  cin >> n;
  for (i = 0; i < n; i++) {
    long long a;
    cin >> a;
    mm.push_back(a);
    sum += a;
  }
  if (sum == 1) {
    cout << -1;
    exit(0);
  }
  long long ssum = sum;
  for (auto i : pr)
    while ((ssum % i) == 0) ssum /= i;
  if (ssum != 1) pr.push_back(ssum);
  long long ans = inf;
  for (auto d : pr)
    if (sum % d == 0) {
      ans = min(solve2(d), ans);
    }
  cout << ans;
  return 0;
}
