#include <bits/stdc++.h>
using namespace std;
long long n;
vector<long long> a(200500, 0);
vector<long long> pref(100500, 0);
long long solve(long long x) {
  set<long long> key_point;
  long long i;
  for (i = 1; i <= n; i++) {
    if ((((pref[i] % x) == (x / 2) + 1) && (a[i])) ||
        ((x == 2) && (pref[i] % 2 == 1)) && (a[i])) {
      key_point.insert(i);
    }
  }
  key_point.insert(-1000000000LL);
  key_point.insert(1000000000LL);
  set<long long>::iterator ii, jj, kk;
  long long answer = 0;
  for (i = 1; i <= n; i++) {
    if (a[i]) {
      if (key_point.find(i) == key_point.end()) {
        key_point.insert(i);
        ii = key_point.find(i);
        jj = ii;
        jj--;
        kk = ii;
        kk++;
        long long prev = *jj;
        long long next = *kk;
        if ((((pref[i] - 1) % x)) >= x / 2)
          answer += i - prev;
        else
          answer += next - i;
        key_point.erase(i);
      }
    }
  }
  return answer;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long sum = 0;
  long long u, j, i;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum == 1) {
    cout << -1;
    return 0;
  }
  for (i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i];
  long long del = 2;
  map<long long, long long> delitel;
  while (sum > 1) {
    while (sum % del == 0) {
      delitel[del]++;
      sum /= del;
    }
    del++;
    if (del * del > sum) break;
  }
  if (sum > 1) delitel[sum]++;
  long long ans = 1000000000000000;
  map<long long, long long>::iterator ii;
  for (ii = delitel.begin(); ii != delitel.end(); ii++) {
    ans = min(ans, solve(ii->first));
  }
  cout << ans;
  return 0;
}
