#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int sum;
int n;
bool prime(int x) {
  for (int i = 2; i <= x / i; i++) {
    if (x % i == 0) return false;
  }
  return true;
}
long long solve(int x) {
  long long res = 0;
  vector<int> b;
  for (int i = 1; i <= n; i++) {
    if (a[i] == 1 && b.size() < x) b.push_back(i);
    if (b.size() == x) {
      for (int j = 0; j < b.size(); j++) res += (long long)abs(b[j] - b[x / 2]);
      b.clear();
    }
  }
  return res;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum == 1) {
    cout << "-1" << endl;
    return 0;
  }
  long long res = 1e15;
  for (int i = 2; i <= sum; i++) {
    if (sum % i == 0 && prime(i)) {
      res = min(res, solve(i));
    }
  }
  cout << res << endl;
  return 0;
}
