#include <bits/stdc++.h>
using namespace std;
int bs(int arr[], int l, int r, int x) {
  while (r >= l) {
    int mid = l + (r - l) / 2;
    if (arr[mid] == x)
      return mid;
    else if (arr[mid] > x)
      r = mid - 1;
    else if (arr[mid] < x)
      l = mid + 1;
  }
  return -1;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int bs(const vector<int> &arr, int l, int r, int x) {
  while (r >= l) {
    int mid = l + (r - l) / 2;
    if (arr[mid] == x)
      return mid;
    else if (arr[mid] > x)
      r = mid - 1;
    else if (arr[mid] < x)
      l = mid + 1;
  }
  return -1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  int temp;
  long long sum = 0;
  vector<int> v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    sum += temp;
    if (temp == 1) v.emplace_back(i);
  }
  if (sum == 1) {
    cout << "-1";
    return 0;
  }
  vector<int> fact;
  temp = sum;
  for (int i = 2; i <= sqrt(temp); i++) {
    if (sum % i == 0) {
      fact.emplace_back(i);
      while (sum % i == 0) sum = sum / i;
    }
  }
  if (sum > 1) fact.emplace_back(sum);
  long long cost, mn = 1e12, k;
  int med;
  for (int i = 0; i < fact.size(); i++) {
    k = 0;
    cost = 0;
    while (k < v.size()) {
      med = v[(2 * k + fact[i] - 1) / 2];
      for (int j = k; j < k + fact[i]; j++) {
        cost += abs(med - v[j]);
      }
      k += fact[i];
    }
    mn = min(mn, cost);
  }
  cout << mn;
  return 0;
}
