#include <bits/stdc++.h>
using namespace std;
long long n, a[1000005], ans = 1e18, x;
long long fun(long long x) {
  long long res = 0;
  for (__typeof((n)) i = (1); i < (n); i++) res += min(a[i] % x, x - a[i] % x);
  return res;
}
void solve() {
  cin >> n;
  for (__typeof((n + 1)) i = (1); i < (n + 1); i++)
    cin >> a[i], a[i] += a[i - 1];
  x = a[n];
  for (__typeof(((long long)sqrt(x) + 1)) i = (2); i < ((long long)sqrt(x) + 1);
       i++) {
    if (x % i == 0) {
      ans = min(ans, fun(i));
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) ans = min(ans, fun(x));
  if (ans == 1e18) ans = -1;
  {
    cout << ans << "\n";
    return;
  };
}
void prep() {}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  prep();
  cout << fixed << setprecision(12);
  while (t--) solve();
  return 0;
}
