#include <bits/stdc++.h>
using namespace std;
template <class T>
void _Print(T &&x) {
  cerr << x << ")"
       << "\n";
}
template <class T, class... S>
void _Print(T &&x, S &&...y) {
  cerr << x << ", ";
  _Print(y...);
}
template <class T>
class compare {
 public:
  bool operator()(pair<T, T> &x, pair<T, T> &y) {
    if (x.first == y.first) {
      return x.second > y.second;
    }
    return x.first > y.first;
  }
};
template <class T>
ostream &operator<<(ostream &os, const pair<T, T> &a) {
  return os << a.first << " " << a.second;
}
long long power(long long a, int b) {
  long long po = 1;
  while (b) {
    if (b & 1) po *= a;
    a *= a;
    b >>= 1;
  }
  return po;
}
template <class T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first + b.first, a.second + b.second};
}
template <class T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first - b.first, a.second - b.second};
}
template <class T>
pair<T, T> operator*(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first * b.first, a.second * b.second};
}
template <class T>
pair<T, T> operator%(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first % b.first, a.second % b.second};
}
template <class T, class U>
pair<T, T> operator+(const pair<T, T> &a, const U &b) {
  return {a.first + b, a.second + b};
}
template <class T, class U>
pair<T, T> operator*(const pair<T, T> &a, const U &b) {
  return {a.first * b, a.second * b};
}
long long Set(long long N, int pos) { return N = N | (1LL << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(long long N, int pos) { return (bool)(N & (1LL << pos)); }
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  long long ara[n];
  long long sum = 0;
  for (auto &x : ara) cin >> x, sum += x;
  if (sum == 1) {
    cout << -1 << "\n";
    return 0;
  }
  long long now = 0, ans = -1;
  long long minans = 1000000000000000000;
  vector<long long> divs;
  long long tempsum = sum;
  long long dv = 2;
  while (dv * dv <= tempsum) {
    if (tempsum % dv == 0) {
      divs.push_back(dv);
      while (tempsum % dv == 0) tempsum /= dv;
    }
    dv++;
  }
  if (tempsum != 1) divs.push_back(tempsum);
  for (auto &i : divs) {
    now = 0, ans = 0;
    long long half = i / 2;
    for (int j = 0; j < n;) {
      if (now - half <= 0) {
        ans += now;
        now += ara[j];
        now %= i;
        j++;
      } else {
        long long k = j, temp = 0, temans = 0;
        long long make = i - now;
        while (k < n) {
          if (ara[k] + temp < make) {
            temans += ara[k] * (k - j + 1);
            temp += ara[k];
            k++;
          } else {
            break;
          }
        }
        if (k >= n) {
          break;
        } else {
          temans += (make - temp) * (k - j + 1);
          ans += temans;
          now = (ara[k] - (make - temp)) % i;
          j = k + 1;
        }
      }
    }
    minans = min(ans, minans);
  }
  cout << minans << "\n";
  return 0;
}
