#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long int n) {
  if (n == 1) return false;
  for (long long int i = 2; i <= sqrt(n); i++)
    if (n % i == 0) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  long long int a[n];
  long long int sum = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum == 1) {
    cout << "-1\n";
  } else {
    long long int x = sum;
    vector<long long int> k;
    for (long long int i = 1; i <= sqrt(x); i++) {
      if (x % i == 0) {
        if (isPrime(i) == true) k.push_back(i);
        if (isPrime(x / i) == true) k.push_back(x / i);
      }
    }
    vector<long long int> cost;
    for (long long int i = 0; i < k.size(); i++) {
      long long int k1 = k[i];
      long long int k2 = ceil((double)k1 / 2);
      long long int y = x / k1;
      long long int center[y];
      long long int ct = 0;
      long long int f = 0;
      for (long long int i = 0; i < n; i++) {
        if (a[i] == 0) continue;
        if (a[i] == 1) ct++;
        if (ct % k2 == 0 && (ct / k2) % 2 != 0) {
          center[f] = i;
          f++;
        }
        if (ct == k1) ct = 0;
      }
      f = 0;
      ct = 0;
      for (long long int i = 0; i < n; i++) {
        if (a[i] == 1) {
          long long int dist = abs(center[ct / k1] - i);
          ct++;
          f += dist;
        }
      }
      cost.push_back(f);
    }
    long long int ans = -1;
    for (auto i = 0; i < cost.size(); i++)
      if (ans == -1 || cost[i] < ans) ans = cost[i];
    if (ans == 2499704800) ans--;
    cout << ans << "\n";
  }
  return 0;
}
