#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int32_t main() {
  const int maxn = 110000;
  bool prime[110000] = {};
  for (long long i = 2; i * i < maxn; i++) {
    if (!prime[i])
      for (int j = i + i; j < maxn; j += i) {
        prime[j] = 1;
      }
  }
  int n;
  cin >> n;
  int a[n];
  int kiek = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    kiek += a[i];
  }
  if (kiek == 1) {
    cout << -1 << '\n';
    return 0;
  }
  auto bandyk = [&](int mid) {
    int turim = 0;
    int start = -1;
    long long ans = 0;
    auto solve = [&](int l, int r) {
      int m = (r - l + 1);
      vector<long long> L(m, 0);
      vector<long long> R(m, 0);
      int turim = 0;
      for (int i = l + 1; i <= r; i++) {
        turim += a[i - 1];
        R[i - l] += R[i - 1 - l] + turim;
      }
      turim = 0;
      for (int i = r - 1; i >= l; i--) {
        turim += a[i + 1];
        L[i - l] += L[i + 1 - l] + turim;
      }
      long long rez = LLONG_MAX;
      for (int i = l; i <= r; i++) {
        rez = min(rez, L[i - l] + R[i - l]);
      }
      ans += rez;
    };
    for (int i = 0; i < n; i++) {
      if (a[i]) {
        if (start == -1) {
          start = i;
        }
        turim++;
        if (turim == mid) {
          solve(start, i);
          turim = 0;
          start = -1;
        }
      }
    }
    return ans;
  };
  long long ans = LLONG_MAX;
  for (int i = 2; i <= kiek; i++) {
    if (!prime[i] && kiek % i == 0) {
      ans = min(ans, bandyk(i));
    }
  }
  cout << ans << '\n';
}
