#include <bits/stdc++.h>
using namespace std;
const int inf_int = 1e9 + 100;
const long long inf_ll = 1e18;
const double pi = 3.1415926535898;
bool debug = 0;
const int MAXN = 1e6 + 100;
const int LOG = 21;
const int mod = 998244353;
const int MX = (1e7 + 100) * 1.5;
const long long MOD = 1000000000949747713ll;
template <class T1, class T2>
std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &rhs) {
  out << "( " << rhs.first << " , " << rhs.second << " )";
  return out;
}
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
vector<long long> get_primes(long long n) {
  vector<long long> res;
  if (n % 2 == 0) {
    res.push_back(2);
  }
  while (n % 2 == 0) {
    n = n / 2;
  }
  for (long long x = 3; x * x <= n; x = x + 2) {
    if (n % x == 0) {
      res.push_back(x);
      n = n / x;
      while (n % x == 0) {
        n = n / x;
      }
    }
  }
  if (n > 1) res.push_back(n);
  return res;
}
int a[MAXN];
long long b[MAXN];
void solve() {
  int n;
  cin >> n;
  vector<int> pos;
  long long sum = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum <= 1) {
    cout << -1;
    return;
  }
  auto primes = get_primes(sum);
  42;
  long long ans = inf_ll;
  for (const long long pr : primes) {
    for (int i = 1; i <= n; ++i) {
      b[i] = a[i] % pr;
    }
    long long cur = 0;
    int last_med = -1;
    int st = 0;
    long long cur_ans = 0;
    for (int i = 1; i <= n; ++i) {
      if (cur == 0 && b[i] > 0) {
        st = i;
      }
      cur += b[i];
      if (2 * cur >= pr && last_med == -1) {
        last_med = i;
      }
      if (cur >= pr) {
        int rest = cur - pr;
        42;
        42;
        b[i] = b[i] - rest;
        for (int j = st; j <= i; ++j) {
          cur_ans += abs(last_med - j) * b[j];
        }
        cur = 0;
        b[i] = rest;
        last_med = -1;
        i--;
      }
    }
    42;
    ans = min(ans, cur_ans);
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(20);
  int t = 1;
  while (t--) solve();
  if (debug)
    cerr << endl << "time : " << (1.0 * clock() / CLOCKS_PER_SEC) << endl;
}
