#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const long long N = 1e5 + 10;
vector<long long> v, pr(N), sf(N);
long long solve(long long m) {
  long long sum = 0, last = 0, ans = 0;
  for (long long i = 0; i < v.size(); i++) {
    sum += v[i];
    if (sum == m) {
      pr[last] = 0;
      long long cnt = v[last];
      for (long long j = last + 1; j <= i; j++) {
        pr[j] = pr[j - 1];
        pr[j] += cnt;
        cnt += v[j];
      }
      cnt = v[i];
      sf[i] = 0;
      for (long long j = i - 1; j >= last; j--) {
        sf[j] = sf[j + 1];
        sf[j] += cnt;
        cnt += v[j];
      }
      long long kek = 1e18;
      for (long long j = last; j <= i; j++) {
        kek = min(kek, sf[j] + pr[j]);
      }
      ans += kek;
      last = i + 1;
      sum = 0;
    }
  }
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    v.push_back(a);
    sum += a;
  }
  long long ans = 1e18;
  if (sum <= 1) {
    cout << -1;
  } else {
    for (long long i = 2; i * i <= (sum); i++) {
      if (sum % i == 0) {
        ans = min(ans, solve(i));
        ans = min(ans, solve(sum / i));
      }
    }
    ans = min(ans, solve(sum));
    cout << ans;
  }
  return 0;
}
