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 INF64 = 1e18;
const int INF = 1e9;
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2>& p) {
  os << "" << p.first << " " << p.second << "";
  return os;
}
template <typename T>
void read(vector<T>& v) {
  int n = v.size();
  for (int i = 0; i < n; i++) cin >> v[i];
}
template <typename T>
vector<T> UNQ(vector<T> a) {
  vector<T> ans;
  set<T> s;
  for (T t : a) s.insert(t);
  for (T t : s) ans.push_back(t);
  return ans;
}
long long ceil(long long a, long long b) {
  long long ans = a / b;
  if (a % b) ans++;
  return ans;
}
long double log(long double a, long double b) { return log(b) / log(a); }
long long power(long long base, long long exp, long long M = LLONG_MAX) {
  long long res = 1;
  while (exp) {
    if (exp % 2 == 1) res = ((res % M) * (base % M)) % M;
    base = ((base % M) * (base % M)) % M;
    exp /= 2;
  }
  return res;
}
string to_base(int n, int new_base) {
  string s;
  int nn = n;
  while (nn) {
    s += to_string(nn % new_base);
    nn /= new_base;
  }
  reverse(s.begin(), s.end());
  return s;
}
long long gcd(long long a, long long b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  long long x = (a / gcd(a, b));
  return b * x;
}
string substr(string s, int l, int r) {
  string ans;
  for (int i = l; i <= r; i++) ans += s[i];
  return ans;
}
long long choose(long long n, long long k, long long mod = 1e9 + 7) {
  long long ans = 1;
  if (k > n - k) k = n - k;
  for (long long i = 0; i < k; ++i) {
    ans = (ans * (n - i)) % mod;
    ans = (ans / (i + 1)) % mod;
  }
  return ans;
}
vector<bool> SIEVE(int n) {
  vector<bool> ans(n + 1);
  for (int i = 2; i <= n; i++) ans[i] = 1;
  for (int i = 2; i * i <= n; i++) {
    if (ans[i])
      for (int j = 2 * i; j <= n; j += i) ans[j] = 0;
  }
  return ans;
}
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) solve();
  return 0;
}
void solve() {
  int n;
  cin >> n;
  vector<long long> a(n);
  read(a);
  int ans = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (a[i] == 1) cnt++;
  if (cnt) {
    cout << n - cnt << "\n";
    return;
  }
  int mn = INF;
  for (int i = 0; i < n; i++) {
    int g = a[i];
    for (int j = i + 1; j < n; j++) {
      g = gcd(g, a[j]);
      if (g == 1) mn = min(mn, j - i + 1);
    }
  }
  if (mn == INF) {
    cout << -1 << "\n";
    return;
  } else {
    cout << n + mn - 2 << "\n";
    return;
  }
}
