#include <bits/stdc++.h>
using namespace std;
int n, sum;
long long ans, res = 1e15;
int a[100005], dd[100005], cnt;
vector<int> uoc;
void SangNT() {
  for (int i = 2; i <= 100000; ++i)
    if (!dd[i])
      for (int j = i; j <= 100000; j += i)
        if (!dd[j]) dd[j] = i;
}
int top, midd;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  SangNT();
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    sum += x;
    if (x) a[++cnt] = i;
  }
  if (sum <= 1) return cout << -1, 0;
  while (sum != 1) {
    int x = dd[sum];
    uoc.push_back(x);
    while (sum % x == 0) sum /= x;
  }
  for (auto v : uoc) {
    ans = 0;
    long long s = 0;
    int mid = (v + 1) / 2;
    for (int i = 1; i <= cnt; ++i) {
      top++;
      if (top == mid) midd = a[i];
      if (top < mid)
        s -= a[i];
      else
        s += a[i];
      if (top == v) {
        s += 1ll * (mid - 1) * midd;
        s -= 1ll * (v - mid + 1) * midd;
        ans += s;
        s = 0;
        top = 0;
      }
    }
    res = min(res, ans);
  }
  cout << res;
}
