You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor).

What is the minimum number of operations you need to make all of the elements equal to 1?

Input

The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array.

The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.

Output

Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.

Examples

Input

5
2 2 3 4 6


Output

5


Input

4
2 4 6 8


Output

-1


Input

3
2 6 9


Output

4

Note

In the first sample you can turn all numbers to 1 using the following 5 moves:

  * [2, 2, 3, 4, 6]. 
  * [2, 1, 3, 4, 6]
  * [2, 1, 3, 1, 6]
  * [2, 1, 1, 1, 6]
  * [1, 1, 1, 1, 6]
  * [1, 1, 1, 1, 1]



We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &A) {
  for (long long i = 0; i < A.size(); i++) os << A[i] << " ";
  os << endl;
  return os;
}
template <>
ostream &operator<<(ostream &os, const vector<vector<long long> > &A) {
  long long N = A.size();
  long long M;
  if (N > 0) M = A[0].size();
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < M; j++) os << A[i][j] << " ";
    os << endl;
  }
  return os;
}
struct edge {
  long long from, to, d, c;
  edge(long long _from = 0, long long _to = 0, long long _d = 0,
       long long _c = 0) {
    from = _from;
    to = _to;
    d = _d;
    c = _c;
  }
  bool operator<(const edge &rhs) const {
    return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
  }
};
struct aabb {
  long long x1, y1, x2, y2;
  aabb(long long x1, long long y1, long long x2, long long y2)
      : x1(x1), y1(y1), x2(x2), y2(y2) {}
};
struct flow {
  long long to, cap, rev, cost;
  flow(long long to = 0, long long cap = 0, long long rev = 0,
       long long cost = 0)
      : to(to), cap(cap), rev(rev), cost(cost) {}
};
const long long di[4] = {0, -1, 0, 1};
const long long dj[4] = {-1, 0, 1, 0};
const long long ci[5] = {0, 0, -1, 0, 1};
const long long cj[5] = {0, -1, 0, 1, 0};
const long long LINF = LLONG_MAX / 2;
const long long INF = INT_MAX / 2;
const long double PI = acos(-1);
long long pow2(long long n) { return 1LL << n; }
template <typename T, typename U>
bool chmin(T &x, const U &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T, typename U>
bool chmax(T &x, const U &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
struct initializer {
  initializer() { cout << fixed << setprecision(20); }
};
initializer _____;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
long long factorial(long long x) {
  if (x == 1)
    return 1;
  else
    return x * factorial(x - 1);
}
long long Mfactorial(long long x, long long m) {
  long long ret = 1;
  for (long long i = 1; i <= x; i++) {
    ret *= i;
    ret %= m;
  }
  return ret;
}
vector<long long> MfactTable(long long n, long long m) {
  vector<long long> ret(n + 1);
  ret[0] = 1;
  long long tmp = 1;
  for (long long i = 1; i <= n; i++) {
    tmp *= i;
    tmp %= m;
    ret[i] = tmp;
  }
  return ret;
}
long long Minverse(long long a, long long m) {
  long long x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}
long long McombSmall(long long n, long long k, long long m) {
  if (k > n) return 0;
  long long ret = Mfactorial(n, m);
  ret *= Minverse(Mfactorial(k, m), m);
  ret %= m;
  ret *= Minverse(Mfactorial(n - k, m), m);
  return ret % m;
}
long long McombFromFactTable(vector<long long> &T, long long n, long long k,
                             long long m) {
  if (k > n) return 0;
  long long ret = T[n];
  ret *= Minverse(T[k], m);
  ret %= m;
  ret *= Minverse(T[n - k], m);
  return ret % m;
}
long long pow(long long x, long long n) {
  if (n == 0) return 1;
  long long ret = (n & 1) ? x : 1;
  long long a = pow(x, n / 2);
  return ret * a * a;
}
long long Mpow(long long x, long long n, long long m) {
  if (n == 0) return 1;
  long long ret = (n & 1) ? x : 1;
  long long a = Mpow(x, n / 2, m);
  return (((ret * a) % m) * a) % m;
}
vector<vector<long long> > combTable(long long N) {
  vector<vector<long long> > ret(N + 1, vector<long long>(N + 1));
  for (long long i = 0; i <= N; i++)
    for (long long j = 0; j <= i; j++) {
      if (j == 0 || j == i)
        ret[i][j] = 1;
      else
        ret[i][j] = ret[i - 1][j - 1] + ret[i - 1][j];
    }
  return ret;
}
long long countNmodPisQlr(long long l, long long r, long long P, long long Q) {
  return (r - Q + P) / P - (l - Q + P - 1) / P;
}
struct Combination {
  long long N = 0, M;
  vector<long long> Ftable, FItable, Inversetable;
  void init() {
    Ftable.resize(N + 1);
    FItable.resize(N + 1);
    Inversetable.resize(N + 1);
    Ftable[0] = 1;
    FItable[0] = 1;
    for (long long i = 1; i <= N; i++) Ftable[i] = Ftable[i - 1] * i % M;
    FItable[N] = Minverse(Ftable[N], M);
    for (long long i = N - 1; i >= 1; i--)
      FItable[i] = FItable[i + 1] * (i + 1) % M;
    Inversetable[1] = 1;
    for (long long i = 2; i <= N; i++)
      Inversetable[i] = M - Inversetable[M % i] * (M / i) % M;
  }
  Combination(long long max, long long mod) {
    N = max;
    M = mod;
    init();
  }
  long long calc(long long n, long long k) {
    if (k > n) return 0;
    long long ret = Ftable[n] * FItable[k] % M * FItable[n - k] % M;
    return ret;
  }
  long long inverse(long long n) { return Inversetable[n]; }
  long long factorial(long long n) { return Ftable[n]; }
  long long factorialInverse(long long n) { return FItable[n]; }
};
struct SegT {
  vector<long long> container;
  long long n;
  void update(long long i, long long x) {
    long long ii = i + n - 1;
    container[ii] = x;
    while (ii > 0) {
      ii = (ii - 1) / 2;
      container[ii] = gcd(container[2 * ii + 1], container[2 * ii + 2]);
    }
  }
  long long get(long long l, long long r, long long l2 = 0, long long r2 = -1,
                long long k = 0) {
    if (r2 == -1) r2 = n;
    if (r2 <= l || r <= l2) return 0;
    if (l <= l2 && r2 <= r) return container[k];
    long long vl = get(l, r, l2, (l2 + r2) / 2, 2 * k + 1);
    long long vr = get(l, r, (l2 + r2) / 2, r2, 2 * k + 2);
    return gcd(vl, vr);
  }
  SegT(vector<long long> &A) {
    n = pow(2, (long long)log2(A.size()) + 1);
    container = vector<long long>(2 * n - 1, 0);
    for (long long i = 0; i < A.size(); i++) update(i, A[i]);
  }
};
long long N, M, K, T, Q, H, W;
signed main() {
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < (long long)(N); i++) cin >> A[i];
  long long s = 0;
  for (long long i = 0; i < (long long)(N); i++) s += (A[i] == 1);
  if (s > 0) {
    cout << N - s << endl;
    return 0;
  }
  SegT sgt(A);
  long long tmp = LINF;
  for (long long l = 0; l < (long long)(N); l++)
    for (long long r = 0; r < (long long)(N + 1); r++)
      if (l < r) {
        long long g = sgt.get(l, r);
        if (g == 1) chmin(tmp, r - l);
      }
  if (tmp == LINF)
    cout << -1 << endl;
  else
    cout << N - 2 + tmp << endl;
  return 0;
}
