#include <bits/stdc++.h>
using namespace std;
const long long inf = 1ll << 61;
long long a[1000006];
long long rwa[1000006];
long long f(long long l, long long r) {
  long long S = rwa[r + 1] - rwa[l];
  long long now = 0;
  for (long long i = l; i <= r; i++) {
    now += a[i];
    if (now == (S + 1) / 2) {
      long long res = 0;
      for (long long j = l; j <= r; j++) res += a[j] * abs(j - i);
      return res;
    }
  }
}
signed main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) rwa[i + 1] = a[i];
  for (long long i = 0; i < n; i++) rwa[i + 1] += rwa[i];
  if (rwa[n] == 1) {
    cout << -1 << endl;
    return 0;
  }
  vector<long long> ya;
  for (long long i = 1; i * i <= rwa[n]; i++) {
    if (rwa[n] % i == 0) {
      if (i != 1) ya.push_back(i);
      if (i * i != rwa[n]) ya.push_back(rwa[n] / i);
    }
  }
  long long ans = inf;
  for (long long d : ya) {
    long long now = 0, l = 0, c = 0;
    for (long long i = 0; i < n; i++) {
      now += a[i];
      if (now == d) {
        c += f(l, i);
        now = 0;
        l = i + 1;
      }
    }
    ans = min(ans, c);
  }
  cout << ans << endl;
}
