#include <bits/stdc++.h>
using namespace std;
long long f(long long a, vector<long long> vec) {
  for (auto& e : vec) {
    e %= a;
  }
  int ind = 0;
  long long ans = 0;
  while (ind < (int)vec.size()) {
    long long cnt = 0;
    long long op = 0;
    for (int i = ind; i < (int)vec.size(); ++i) {
      cnt += vec[i];
      if (cnt >= ((a + 1) / 2)) {
        op = i;
        break;
      }
    }
    if (cnt == 0) {
      break;
    }
    cnt = 0;
    for (int i = ind; i < (int)vec.size(); ++i) {
      long long s = min(a - cnt, vec[i]);
      ans += s * abs(i - op);
      vec[i] -= s;
      cnt += s;
      if (cnt == a) {
        ind = i;
        break;
      }
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> vec(n);
  long long sm = 0;
  for (int i = 0; i < n; ++i) {
    cin >> vec[i];
    sm += vec[i];
  }
  if (sm <= 1) {
    cout << -1 << endl;
    return 0;
  }
  vector<long long> rz;
  long long cn = sm;
  for (long long i = 2; i * i <= cn; ++i) {
    while (cn % i == 0) {
      rz.push_back(i);
      cn /= i;
    }
  }
  if (cn > 1) {
    rz.push_back(cn);
  }
  sort(rz.begin(), rz.end());
  rz.erase(unique(rz.begin(), rz.end()), rz.end());
  long long ans = 1e18 + 100;
  for (auto e : rz) {
    ans = min(ans, f(e, vec));
  }
  cout << ans << endl;
  return 0;
}
