#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum == 1) {
    cout << -1;
    return 0;
  }
  long long gl_ans = 1e18;
  for (int k = 2; k <= sum; k++) {
    if (sum % k) continue;
    while (sum % k == 0) sum /= k;
    long long ans = 0;
    for (int p = 0; p < n; p++) {
      if (a[p] == 0) continue;
      long long cur = 0;
      int took = 0;
      while (took < (k + 1) / 2) {
        cur += took;
        took += a[p++];
      }
      p--;
      int mid = p;
      int all = took;
      while (all < k) all += a[++p];
      took = 0;
      for (int j = p; j >= mid; j--) {
        cur += took;
        took += a[j];
      }
      ans += cur;
    }
    gl_ans = min(gl_ans, ans);
  }
  cout << gl_ans << endl;
}
