#include <bits/stdc++.h>
long long powermod(long long _a, long long _b, long long _m) {
  long long _r = 1;
  while (_b) {
    if (_b % 2 == 1) _r = (_r * _a) % _m;
    _b /= 2;
    _a = (_a * _a) % _m;
  }
  return _r;
}
using namespace std;
template <typename T>
void pr(const T& t) {
  cout << t << "\n";
}
template <typename T, typename U, typename... ARGS>
void pr(const T& t, const U& u, const ARGS&... args) {
  cout << t << " ";
  pr(u, args...);
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  long long n;
  cin >> n;
  ;
  long long arr[n], sum = 0;
  for (long long i = 0; i < n; ++i) {
    cin >> arr[i];
    ;
    sum += arr[i];
  }
  if (sum == 1)
    pr(-1);
  else {
    long long ans = 0, k, fans = 1e18;
    set<long long> factors;
    for (long long i = 2; i <= sum; i++)
      while (sum % i == 0) {
        factors.insert(i);
        sum /= i;
      }
    vector<long long> v;
    for (auto it : factors) {
      ans = 0;
      k = it;
      for (long long i = 0; i < n; ++i) {
        if (arr[i] == 1) v.push_back(i);
        if (v.size() == k) {
          long long mid = k / 2;
          long long mid_element = v[mid];
          for (long long j = 0; j < k; ++j) ans += abs(v[j] - mid_element);
          v.clear();
        }
      }
      fans = min(ans, fans);
    }
    pr(fans);
  }
}
