#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 2e5 + 10;
const ll inf = 1e15 + 42;
const ll mod = 1000000007;
vector<int> prime;
void Calc(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      prime.push_back(i);
      while (n % i == 0) {
        n = n / i;
      }
    }
  }
  if (n) prime.push_back(n);
}
void testCase() {
  int n;
  cin >> n;
  int val;
  vector<int> pos;
  for (int i = 0; i < n; i++) {
    cin >> val;
    if (val == 1) pos.push_back(i);
  }
  if (n == 1 || pos.size() <= 1) {
    cout << "-1"
         << "\n";
    return;
  }
  Calc(pos.size());
  ll op = 0;
  int i = 1;
  int j = pos.size() - 1;
  ll cnt = 1;
  while (i <= j) {
    if (i != j) {
      op += (cnt * (pos[i] - pos[i - 1]));
      i++;
      op += (cnt * (pos[j] - pos[j - 1]));
      j--;
    } else if (i == j) {
      op += (cnt * (pos[i] - pos[i - 1]));
      i++;
      j--;
    }
    cnt++;
  }
  ll ans = op;
  for (int l = 0; l < prime.size(); l++) {
    int size = prime[l];
    int prev = 1;
    ll op = 0;
    while (prev < pos.size()) {
      int i, j;
      i = prev;
      j = i + size - 2;
      ll cnt = 1;
      while (i <= j) {
        if (i != j) {
          op += (cnt * (pos[i] - pos[i - 1]));
          i++;
          op += (cnt * (pos[j] - pos[j - 1]));
          j--;
        } else if (i == j) {
          op += (cnt * (pos[i] - pos[i - 1]));
          i++;
          j--;
        }
        cnt++;
      }
      prev += size;
    }
    if (op) ans = min(ans, op);
  }
  cout << ans << "\n";
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  testCase();
  return 0;
}
