#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long int n, a[200005], ans = 0;
vector<long long int> v;
int32_t main() {
  ios_base::sync_with_stdio();
  cin.tie();
  cout.tie();
  ;
  cin >> n;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1) {
      v.push_back(i);
    }
  }
  long long int x = 0;
  if (n == 1) {
    return cout << "-1", 0;
    return 0;
  }
  if (v.size() <= 1) {
    return cout << "-1", 0;
    return 0;
  }
  long long int fn = 1e12;
  for (long long int c = 2; c <= v.size(); c++)
    if (v.size() % c == 0) {
      x = c;
      ans = 0;
      for (long long int i = 0; i < v.size(); i = i + x) {
        long long int y;
        long long int s1 = 0, s2 = 0;
        if (x % 2 == 1) {
          y = v[i + x / 2];
          for (long long int j = i; j < i + x; j++) ans = ans + abs(v[j] - y);
        } else {
          y = v[i + x / 2];
          s1 = 0;
          for (long long int j = i; j < i + x; j++) s1 = s1 + abs(v[j] - y);
          y = v[i + x / 2 - 1];
          s2 = 0;
          for (long long int j = i; j < i + x; j++) s2 = s2 + abs(v[j] - y);
          ans = ans + min(s1, s2);
        }
      }
      fn = min(fn, ans);
    }
  cout << fn << endl;
}
