#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100005;
long long ar[maxn];
long long n;
long long pref[maxn], pref2[maxn];
long long get(long long f) {
  long long li = 0, temp = 0, s = 0;
  for (long long j = 0; j < n; j++) {
    s += ar[j];
    if (s == f) {
      vector<long long> v;
      for (long long k = li; k <= j; k++)
        if (ar[k]) v.push_back(k);
      pref[0] = 0;
      for (long long i = 0; i < f; i++) pref[i + 1] = pref[i] + v[i];
      long long t2 = 1000000000000007;
      for (long long i = 0; i < f; i++)
        t2 = min(t2, i * v[i] - pref[i] + pref[f] - pref[i + 1] -
                         (f - i - 1) * v[i]);
      temp += t2;
      s = 0;
      li = j + 1;
    }
  }
  return temp;
}
int32_t main() {
  cin >> n;
  long long sum = 0;
  for (long long i = 0; i < n; sum += ar[i], i++) cin >> ar[i];
  if (sum == 1) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 1000000000000007;
  for (long long i = 2; i * i <= sum; i++) {
    if (sum % i) continue;
    ans = min(ans, get(i));
    ans = min(ans, get(sum / i));
  }
  ans = min(ans, get(sum));
  cout << ans << endl;
}
