#include <bits/stdc++.h>
clock_t time_p = clock();
using namespace std;
const int N = 1e6 + 1, mod = 998244353;
int n, a[N];
long long int b[N];
long long int solve(long long int k) {
  long long int ans = 0;
  for (int i = 0; i < n; i++) ans += min(b[i] % k, k - b[i] % k);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = (i ? b[i - 1] : 0) + a[i];
  }
  long long int ans = 1e18, tot = b[n - 1];
  for (long long int i = 2; i * i <= tot; i++) {
    if (tot % i == 0) {
      while (tot % i == 0) tot /= i;
      ans = min(ans, solve(i));
    }
  }
  if (tot > 1) ans = min(ans, solve(tot));
  cout << (ans == 1e18 ? -1 : ans);
  cerr << "\nTime Taken : " << (float)(clock() - time_p) / CLOCKS_PER_SEC
       << "\n";
  ;
}
