#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 200;
template <class T>
inline void read(T &x) {
  x = 0;
  char ch = getchar(), w = 0;
  while (!isdigit(ch)) w = (ch == '-'), ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  x = w ? -x : x;
  return;
}
int n;
long long ans = 1e18, sum[N], tmp;
inline long long cal(long long a) {
  long long tot = 0;
  for (register int i = 1; i <= n; i++) tot += min(sum[i] % a, a - sum[i] % a);
  return tot;
}
int main() {
  read(n);
  for (register int i = 1; i <= n; i++) read(sum[i]), sum[i] += sum[i - 1];
  tmp = sum[n];
  if (tmp == 1) {
    cout << -1;
    return 0;
  }
  for (register long long i = 2; i * i <= tmp; i++) {
    if (tmp % i == 0) {
      ans = min(ans, cal(i));
      while (tmp % i == 0) tmp /= i;
    }
  }
  if (tmp > 1) ans = min(ans, cal(tmp));
  cout << ans << endl;
  return 0;
}
