#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const long long LINF = 1e18;
const double pi = 3.1415926535897932;
const string endstr = "\n";
template <typename T>
T gcd(T a, T b) {
  return (a == 0) ? b : gcd(b % a, a);
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
bool p_comp_fs(const pair<long long, long long> p1,
               const pair<long long, long long> p2) {
  return p1.first < p2.first;
};
bool p_comp_fg(const pair<long long, long long> p1,
               const pair<long long, long long> p2) {
  return p1.first > p2.first;
};
bool p_comp_ss(const pair<long long, long long> p1,
               const pair<long long, long long> p2) {
  return p1.second < p2.second;
};
bool p_comp_sg(const pair<long long, long long> p1,
               const pair<long long, long long> p2) {
  return p1.second > p2.second;
};
template <typename T>
vector<T> uniquen(vector<T> vec) {
  sort((vec).begin(), (vec).end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  return vec;
}
inline long long popcnt(long long x) {
  return __builtin_popcountll((unsigned long long)x);
};
template <class T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
map<long long, long long> prime_factor(int64_t n) {
  map<long long, long long> ret;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1) ret[n] = 1;
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) cin >> A[i];
  vector<long long> S(N + 1, 0);
  for (long long i = 0; i < N; i++) S[i + 1] = S[i] + A[i];
  if (S[N] == 1) {
    cout << -1 << endstr;
    return 0;
  }
  vector<long long> cand;
  auto pf = prime_factor(S[N]);
  for (auto it = pf.begin(); it != pf.end(); it++) {
    cand.push_back(it->first);
  }
  long long ans = LINF;
  for (auto c : cand) {
    if (c == 1) continue;
    long long res = 0;
    for (long long i = (1); i < N; i++) {
      long long uc = (S[i] / c + 1) * c;
      long long lc = S[i] / c * c;
      res += min(uc - S[i], S[i] - lc);
    }
    chmin(ans, res);
  }
  cout << ans << endstr;
  return 0;
}
