#include <bits/stdc++.h>
using namespace std;
int const N = 1e6 + 10;
int oo = 1e9;
int mod = oo + 7;
long long OO = 3e18;
int n, aa[N];
int sum = 0;
vector<int> pos;
vector<long long> cum;
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> aa[i], sum += aa[i];
    if (aa[i]) pos.push_back(i);
  }
  cum.resize(n + 2);
  for (int i = 1; i <= (int)pos.size(); i++) cum[i] = cum[i - 1] + pos[i - 1];
  if (sum == 1) {
    cout << -1;
    return;
  }
  vector<int> dv;
  dv.push_back(sum);
  for (int i = 2; i * i <= sum; i++) {
    if (sum % i == 0) {
      dv.push_back(i);
      dv.push_back(sum / i);
    }
  }
  long long an = OO;
  for (int i = 0; i < (int)dv.size(); i++) {
    int a = dv[i];
    long long tr = 0;
    for (int j = 0; j < (int)pos.size(); j += a) {
      long long res = OO;
      for (int k = j; k < a + j; k++) {
        long long need = pos[k] * 1LL * (k - j);
        need -= cum[k] - cum[j];
        need += cum[a + j] - cum[k + 1];
        need -= pos[k] * 1LL * (a + j - k - 1);
        res = min(res, need);
      }
      tr += res;
    }
    an = min(an, tr);
  }
  cout << an << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}
