#include <bits/stdc++.h>
using namespace std;
const unsigned long long int mod = (long long int)1e9 + 7;
const long long int inf = (long long int)1e17 + 42;
const long long int N = (long long int)1e6 + 2;
long long int power(long long int x, unsigned long long int y) {
  long long int res = 1;
  while (y > 0) {
    if (y & 1) res = (res * x) % mod;
    y = y >> 1;
    x = (x * x);
  }
  return res % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  t = 1;
  while (t--) {
    long long int n;
    cin >> n;
    long long int arr[n], cnt = 0;
    for (auto& x : arr) cin >> x, cnt += x;
    set<long long int> s;
    if (cnt == 1) {
      cout << -1 << '\n';
      return 0;
    }
    for (long long int i = 2; i * i <= cnt; i++) {
      if (cnt % i == 0) s.insert(i), s.insert(cnt / i);
    }
    s.insert(cnt);
    long long int ans = inf;
    for (auto x : s) {
      vector<long long int> v;
      long long int tnp = 0;
      for (long long int i = 0; i < n; i++) {
        if (arr[i]) {
          v.push_back(i);
          if (v.size() == x) {
            long long int med = x / 2;
            for (long long int j = 0; j < x; j++) tnp += abs(v[j] - v[med]);
            v.clear();
          }
        }
      }
      ans = min(ans, tnp);
    }
    cout << ans << '\n';
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
