#include <bits/stdc++.h>
using namespace std;
long long n, cc, t, res, ans, a[1000010];
long long dem[1000010], SR[1000010], SL[1000010];
long long BS(long long w) {
  long long t = n, l, r, mid;
  l = 1;
  r = n;
  while (l <= r) {
    mid = (l + r) / 2;
    if (dem[mid] >= w) {
      t = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  return t;
}
void tinh(long long w) {
  long long l, r, mid;
  ans = 0;
  t = 0;
  while (t < n && dem[t] < cc) {
    l = t + 1;
    mid = BS(dem[t] + (w + 1) / 2);
    r = BS(dem[t] + w);
    ans += SL[mid] - SL[l] - dem[l - 1] * (mid - l);
    ans += SR[mid] - SR[r] - (dem[n] - dem[r]) * (r - mid);
    t = r;
  }
  res = min(res, ans);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  scanf("%d", &n);
  for (long long i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (long long i = 1; i <= n; i++) {
    SL[i] = SL[i - 1] + dem[i - 1];
    dem[i] = dem[i - 1] + a[i];
  }
  cc = 0;
  for (long long i = n; i >= 1; i--) {
    SR[i] = SR[i + 1] + cc;
    cc += a[i];
  }
  if (cc <= 1) {
    cout << -1;
    return 0;
  }
  res = 1e15;
  tinh(cc);
  for (long long o = 2; o <= trunc(sqrt(cc)); o++)
    if (cc % o == 0) {
      tinh(o);
      tinh(cc / o);
    }
  cout << res;
}
