#include <bits/stdc++.h>
using namespace std;
const long long INF = 1E18;
const int maxn = 100010;
int box[maxn], check[maxn];
long long func(int num, int &N) {
  long long res = 0;
  int boxptr = 0, checkptr;
  while (boxptr < N) {
    while (boxptr < N && !box[boxptr]) boxptr++;
    if (boxptr >= N) break;
    checkptr = 0;
    while (checkptr < num) {
      while (!box[boxptr]) boxptr++;
      check[checkptr++] = boxptr++;
    }
    int mid = check[num / 2];
    for (int j = 0; j < num; j++) res += abs(check[j] - mid);
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> box[i];
    if (box[i] > 0) cnt++;
  }
  long long res = INF;
  for (int i = 2; i <= cnt; i++) {
    if (cnt % i) continue;
    res = min(res, func(i, n));
  }
  if (res == INF)
    cout << "-1" << endl;
  else
    cout << res << endl;
  return 0;
}
