#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 10;
int n, m;
int a[mx];
long long pre[mx];
vector<int> g;
int main() {
  scanf("%d", &n);
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    sum += a[i];
    if (a[i]) {
      g.push_back(i);
      pre[g.size()] = pre[g.size() - 1] + i;
    }
  }
  long long ans = 1e16;
  int ti = 0;
  for (int i = 2; i <= n; i++) {
    if (ti == 10) break;
    if (sum % i == 0) {
      long long ret = 0;
      for (int j = 0; j + i <= g.size(); j += i) {
        int bg = g[j];
        int ed = g[j + i - 1];
        int mid = g[(2 * j + i - 1) / 2];
        int k = (2 * j + i - 1) / 2;
        ret += 1ll * (k - j) * mid - (pre[k] - pre[j]);
        ret += (pre[j + i] - pre[k]) - 1ll * (j + i - k) * mid;
      }
      ans = min(ans, ret);
      ti++;
    }
  }
  printf("%lld\n", ans == 1e16 ? -1 : ans);
  return 0;
}
