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;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
namespace ib {
char b[100];
}
inline void pi(int x) {
  if (x == 0) {
    putchar(48);
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  char *s = ib::b;
  while (x) *(++s) = x % 10, x /= 10;
  while (s != ib::b) putchar((*(s--)) + 48);
}
inline void ri(int &x) {
  x = 0;
  static char c;
  bool t(0);
  while (c = getchar(), c < '0' || c > '9')
    if (c == '-')
      t = 1;
    else
      t = 0;
  do x = (x << 1) + (x << 3) + c - '0';
  while (c = getchar(), c >= '0' && c <= '9');
  if (t) x = -x;
}
vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c)) v.emplace_back(x);
  return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  if (0)
    cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\n';
  err(++it, args...);
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int32_t main() {
  int n;
  cin >> n;
  if (n == 1) {
    int e;
    cin >> e;
    if (e == 1) return cout << 0, 0;
    return cout << -1, 0;
  }
  vector<int> a(n);
  vector<int> left(n);
  vector<int> right(n);
  int res = n;
  bool stop = 0;
  for (int i = 0; i < n; i++) {
    int e;
    ri(e);
    a[i] = e;
    if (e == 1) {
      stop = 1;
      res--;
    }
  }
  if (stop) return cout << res, 0;
  int m = 1e9;
  for (int i = 0; i < n; i++) {
    int t = gcd(a[i], a[i]);
    for (int j = i + 1; j < n; j++) {
      t = gcd(t, a[j]);
      if (t == 1) {
        stop = 1;
        m = min(m, j - i);
      }
    }
  }
  if (!stop) return cout << -1, 0;
  return cout << n - 1 + m, 0;
}
