#include <bits/stdc++.h>
using namespace std;
vector<long long> divi;
void di(long long n) {
  for (long long i = 2; i <= n; i++)
    if (n % i == 0) divi.push_back(i);
}
int main() {
  long long n, sum = 0;
  cin >> n;
  vector<long long> arr, a;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    arr.push_back(x);
    sum += x;
  }
  di(sum);
  if (divi.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  long long res = 1e18, min1 = 0, min2 = 0, min3 = 0, ans = 0;
  for (long long i = 0; i < divi.size(); i++) {
    long long cnt = 0;
    min1 = 0, min2 = 0, min3 = 0, ans = 0;
    for (long long j = 0; j < arr.size(); j++) {
      if (arr[j]) {
        cnt++;
        a.push_back(j + 1);
      }
      if (cnt == divi[i]) {
        long long mid1 = (a[a.size() - 1] + a[0]) / 2, zer = 0;
        long long mid2 = mid1 + 1;
        long long mid3 = max(zer, mid1 - 1);
        mid1 = a[a.size() / 2];
        for (long long k = 0; k < a.size(); k++) {
          min1 += abs(a[k] - mid1);
        }
        a.clear();
        cnt = 0;
      }
    }
    res = min(res, min1);
  }
  cout << res << endl;
}
