#include <bits/stdc++.h>
using namespace std;
void err() { cout << endl; }
template <class T, class... Ts>
void err(const T& arg, const Ts&... args) {
  cout << arg << ' ';
  err(args...);
}
using ll = long long;
using db = double;
using pII = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 10;
int n;
int a[N];
ll s[N];
ll f(ll p) {
  ll res = 0;
  for (int i = 1; i <= n; ++i) {
    res += min(s[i] % p, p - s[i] % p);
  }
  return res;
}
void RUN() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
  }
  if (s[n] == 1) {
    cout << -1 << "\n";
    return;
  }
  ll res = INFLL, sum = s[n];
  for (ll x = 2; x * x <= sum; ++x) {
    if (sum % x == 0) {
      while (sum % x == 0) {
        sum /= x;
      }
      res = min(res, f(x));
    }
  }
  if (sum > 1) res = min(res, f(sum));
  cout << res << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(20);
  int T = 1;
  while (T--) {
    RUN();
  }
  return 0;
}
