#include <bits/stdc++.h>
using namespace std;
long long n, sum, k, ans, c;
long long arr[1000000 + 5];
long long tmp[1000000 + 5];
vector<long long> frac;
signed main() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> arr[i], sum += arr[i];
  }
  ans = 1e18;
  for (long long i = 2; i <= sqrt(sum); i++) {
    if (sum % i == 0) frac.push_back(i), frac.push_back(sum / i);
  }
  frac.push_back(1);
  for (long long i : frac) {
    k = sum / i, c = 0;
    for (long long j = 0; j < n; j++) tmp[j] = 0;
    for (long long j = 0; j < n; j++) {
      tmp[j] += arr[j];
      if (tmp[j] < 0) {
        c += -tmp[j];
        tmp[j + 1] += tmp[j];
        tmp[j] = 0;
      }
      if (tmp[j] % k != 0) {
        if (tmp[j] % k < k - tmp[j] % k) {
          c += tmp[j] % k;
          tmp[j + 1] += tmp[j] % k;
        } else {
          c += k - tmp[j] % k;
          tmp[j + 1] -= k - tmp[j] % k;
        }
      }
    }
    ans = min(ans, c);
  }
  if (sum > 1)
    cout << ans << '\n';
  else
    cout << -1 << '\n';
}
