#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long m);
long long modInverse(long long n, long long m);
long long nCr(long long n, long long r);
long long ceiling(long long x, long long y);
long long check(vector<int>& a, int f) {
  long long ans = 0;
  vector<int> pos;
  for (int i = 0; i < (long long)a.size(); i++) {
    if (a[i] == 1) pos.push_back(i);
    if ((long long)pos.size() == f) {
      int mid = f / 2;
      for (int p : pos) ans += abs(p - pos[mid]);
      pos.clear();
    }
  }
  return ans;
}
void aksayushx() {
  int n;
  cin >> n;
  vector<int> a(n);
  int s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  set<int> factors;
  for (int i = 2; i <= s; i++) {
    int c = 0;
    while (s % i == 0) {
      c++;
      s /= i;
    }
    if (c > 0) factors.insert(i);
  }
  long long ans = 1e18;
  for (int f : factors) ans = min(ans, check(a, f));
  ans = ans == 1e18 ? -1 : ans;
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) aksayushx();
  return 0;
}
long long power(long long x, long long y, long long m) {
  long long res = 1;
  x = x % m;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res;
}
long long modInverse(long long n, long long m) { return power(n, m - 2, m); }
long long ceiling(long long x, long long y) { return (x + y - 1) / y; }
