#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar())) x = x * 10 + c - 48;
  if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
  if (x > 9) writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
  putchar(' ');
}
template <typename T>
inline void writeln(T x) {
  write(x);
  putchar('\n');
}
using namespace std;
const long long mod = 1e18;
const long pp = 2e6;
long long a[pp];
int n, Qtion, n1;
vector<int> p;
long long sum, ans, m;
string S, X;
vector<int> VT;
int getbit(int bit, int ii) { return (bit >> ii) & 1; }
long long get(long long k) {
  long long res = 0;
  for (int i = 1; i <= n; ++i) {
    res = res + (long long)min(a[i] % k, k - a[i] % k);
  }
  return res;
}
void dowork() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
    a[i] += a[i - 1];
  }
  m = a[n];
  if (m == 1) {
    cout << -1;
    return;
  }
  ans = mod;
  for (long long dv = 2; (dv * dv <= m); ++dv)
    if (m % dv == 0) {
      ans = min(ans, get(dv));
      while (m % dv == 0) m /= dv;
    }
  if (m != 1) ans = min(ans, get(m));
  cout << ans;
}
void openmain() {
  ios_base::sync_with_stdio(0);
  freopen(
      "1255E1"
      ".inp",
      "r", stdin);
  cout.tie();
}
int main() {
  dowork();
  return 0;
}
