#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const string nl = "\n";
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (auto& i : a) {
    cin >> i;
  }
  auto factor = [&](long long x) {
    deque<long long> hld;
    for (long long i = 1; i * i <= x; ++i) {
      if (x % i == 0) {
        hld.push_back(i);
        if (i * i != x) {
          hld.push_back(x / i);
        }
      }
    }
    return hld;
  };
  vector<long long> hld;
  for (long long i = 0; i < ((long long)(a).size()); ++i) {
    if (a[i] == 1) {
      hld.push_back(i);
    }
  }
  deque<long long> hld1 = factor(accumulate(a.begin(), a.end(), 0LL));
  sort(hld1.begin(), hld1.end());
  hld1.pop_front();
  long long ans = INF;
  for (long long i = 0; i < ((long long)(hld1).size()); ++i) {
    long long hld2 = hld1[i] / 2;
    long long ans1 = 0;
    for (long long j = 0; j <= ((long long)(hld).size()) - hld1[i];
         j += hld1[i]) {
      for (long long h = j; h < j + hld1[i]; ++h) {
        assert(h < ((long long)(hld).size()) && "h must be < si(hld)");
        ans1 += abs(hld[j + hld2] - hld[h]);
      }
    }
    ans = min(ans, ans1);
  }
  cout << (ans == INF ? -1 : ans) << nl;
}
