#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int n, sum;
vector<int> mult;
vector<long long> ind;
long long get(vector<long long> &cur) {
  int m = cur.size();
  int mid = m / 2;
  long long res = 0;
  for (int i = 0; i < m; i++) res += abs(cur[mid] - cur[i]);
  return res;
}
long long calc(int p) {
  vector<long long> cur;
  long long res = 0;
  for (int i = 0; i < ind.size(); i += p) {
    cur.clear();
    for (int j = i; j < i + p; j++) cur.push_back(ind[j]);
    res += get(cur);
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += x;
    if (x) ind.push_back((long long)i);
  }
  if (sum == 1) return cout << -1, 0;
  for (int i = 2; i * i <= sum; i++)
    if (sum % i == 0) {
      mult.push_back(i);
      if (sum / i != 1) mult.push_back(sum / i);
    }
  mult.push_back(sum);
  long long ans = 1e18;
  for (int i = 0; i < mult.size(); i++) ans = min(ans, calc(mult[i]));
  cout << ans;
}
