#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXN = 1e6 + 5;
int arr[MAXN], n;
ll fun(ll x) {
  if (x == 1) return INF;
  ll sum = 0, y = 0;
  for (int i = 0; i < n; i++) {
    y += arr[i];
    y %= x;
    sum += min(y, x - y);
  }
  return sum;
}
int main() {
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ll sum = 0, ans = INF;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i], sum += arr[i];
  if (sum < 2) {
    cout << -1 << '\n';
    return 0;
  }
  for (ll i = 2; i * i <= sum; i++) {
    if (sum % i == 0) ans = min(ans, fun(i));
    while (sum % i == 0) sum /= i;
  }
  ans = min(ans, fun(sum));
  cout << ans << '\n';
  return 0;
}
