#include <bits/stdc++.h>
using namespace std;
const int M = 1200000;
const long long inf = 1e18;
int n, m, k = 0;
vector<int> v;
int fac[M];
void get_fac(int x) {
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      fac[k++] = i;
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) fac[k++] = x;
}
bool vs[M] = {0};
long long fj(int l, int r) {
  long long sum = 0;
  long long ret = inf;
  long long num = 0;
  long long c = 0;
  for (int i = (l); i <= (r); ++i) {
    sum += (vs[i] ? i : 0);
    num += vs[i];
  }
  for (int i = (l); i <= (r); ++i) {
    ret = min(ret, sum + c * i * 2 - num * i);
    if (vs[i]) {
      sum -= (i << 1);
      ++c;
    }
  }
  return ret;
}
long long cal(int x) {
  long long ret = 0;
  for (int i = 0; i < m; i += x) {
    ret += fj(v[i], v[i + x - 1]);
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  int u;
  for (int i = (1); i <= (n); ++i) {
    cin >> u;
    if (u) {
      v.push_back(i);
      vs[i] = 1;
    }
  }
  m = v.size();
  get_fac(m);
  if (k == 0) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = inf;
  for (int i = (0); i <= (k - 1); ++i) {
    ans = min(ans, cal(fac[i]));
  }
  cout << ans << endl;
  return 0;
}
