#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5 + 5;
int a[sz];
vector<int> combs[sz];
int main() {
  long long fin = 1e18;
  int n, tot = 0;
  cin >> n;
  for (int i = 0; i < n; i++) scanf("%d", &a[i]), tot += a[i];
  for (int i = 2; i <= tot; i++) {
    if (tot % i == 0) {
      for (int j = 0, k = 0; j < n; j++) {
        if (a[j]) {
          combs[k / i].push_back(j);
          k++;
        }
      }
      long long ans = 0;
      for (int j = 0; j < tot / i; j++) {
        int mid = combs[j][i / 2];
        for (int k = 0; k < combs[j].size(); k++) {
          ans += abs(combs[j][k] - mid);
        }
        combs[j].clear();
      }
      fin = min(fin, ans);
    }
  }
  if (fin == 1e18) fin = -1;
  cout << fin << endl;
}
