#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
long long n, a[100010], sum[100010];
vector<long long> v;
signed main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i] * i;
    if (a[i] == 1) v.push_back(i);
  }
  if (v.size() == 1) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 9000000000000000007ll, len = v.size();
  for (long long i = 2; i <= len; i++)
    if (len % i == 0) {
      long long l = 0, r = i - 1, num = 0;
      while (r <= len - 1) {
        long long val = (l + r) / 2;
        long long mid = v[val];
        num += (sum[v[r]] - sum[mid]) - mid * (r - val);
        num += mid * (val - l) - (sum[mid - 1] - sum[v[l] - 1]);
        r += i;
        l += i;
      }
      ans = min(ans, num);
    }
  cout << ans << endl;
  return 0;
}
