#include <bits/stdc++.h>
using namespace std;
vector<int> a;
long long find_cost(int k) {
  long long cost = 0;
  for (int i = 0; i < (int)a.size() / k; ++i) {
    int l = i * k;
    int r = l + k;
    int median = a[(l + r) / 2];
    for (int j = l; j < r; ++j) {
      cost += abs(median - a[j]);
    }
  }
  return cost;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (x > 0) {
      a.push_back(i);
    }
  }
  if (a.size() == 1) {
    cout << "-1\n";
    exit(0);
  }
  long long min_cost = LLONG_MAX;
  for (int k = 1; k * k <= (int)a.size(); ++k) {
    if ((int)a.size() % k == 0) {
      if (k != 1) {
        min_cost = min(min_cost, find_cost(k));
      }
      if (a.size() / k != k) {
        min_cost = min(min_cost, find_cost((int)a.size() / k));
      }
    }
  }
  cout << min_cost << '\n';
  return 0;
}
