#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long long INF = 9 * 1e9 + 7;
const int base = 1e9;
const int MAX = 1e5 + 20;
const double EPS = 1e-9;
const double PI = acos(-1.);
const long long O = pow(2ll, 32) - 1;
int cnt[MAX], a[MAX][3], res[MAX], was[MAX];
vector<vector<int> > is(MAX);
int main() {
  int n, p = 2, cnt = 0;
  long long res = INF;
  cin >> n;
  vector<int> v(n), dil;
  for (int i = (0); i < (n); ++i) {
    cin >> v[i];
    cnt += v[i];
  }
  if (cnt == 1) {
    cout << -1;
    return 0;
  }
  for (int i = (2); i < (cnt + 1); ++i) {
    if (cnt % i == 0) {
      p = i;
      dil.push_back(i);
    }
  }
  for (auto d : dil) {
    long long cur = 0, x = 0;
    vector<int> pos;
    for (int i = (0); i < (n); ++i) {
      if (v[i] == 1) {
        pos.push_back(i);
        x++;
      }
      if (x == d) {
        int mid = (int)pos.size() / 2;
        for (auto p : pos) {
          cur += abs(p - pos[mid]);
        }
        pos.clear();
        x = 0;
      }
    }
    res = min(res, cur);
  }
  cout << res;
  return 0;
}
