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.
import java.util.*;
import java.awt.Point;
import java.io.*;

public class Main{
    public static void read(long[] a,int n) throws IOException{for (int i=0;i<n;i++){a[i]=Reader.nextLong();}}
    public static <T> void print(T[] a,int n,PrintWriter out){for (T i : a){ out.print(i+" ");}out.println();}
    public static void main(String[] args) throws IOException{
        Reader scan = new Reader();
        scan.init(System.in);
        // BufferedReader br = new BufferedReader(new FileReader("input.txt"));
        OutputStream output = System.out;
        // BufferedWriter output = new BufferedWriter(new FileWriter("output.txt"));
        PrintWriter out = new PrintWriter(output);
        int N = scan.nextInt();
        int[] arr = new int[N];
        int count = 0;
        for (int i=0;i<N;i++){
            arr[i] = scan.nextInt();
            if (arr[i]!=1){
                count++;
            }
        }
        if (count<N){
            out.println(count);
            out.close();
            return;
        }
        SegmentTree tree = new SegmentTree(N);
        tree.make(arr,0,N-1,0);
        int ans = Integer.MAX_VALUE;
        int i=0,j=0;
        for(;i<N;i++){
            int A = tree.query(0, N-1,0, i, j);
            while(j<N && A!=1){
                j++;
                A = tree.query(0, N-1,0, i, j);
            }
            if (j==N){
                break;
            }
            ans = Math.min(ans,j-i+1);
        }
        if (ans==Integer.MAX_VALUE){
            out.println(-1);
        }
        else{
            out.println(ans-1+N-1);
        }
        out.close();
    }
}
class SegmentTree{
    int[] Arr;
    SegmentTree(int N){
        Arr = new int[4*N];
    }
    public static int gcd(int a,int b){
        if (b==0){
            return a;
        }
        else{
            return gcd(b, a%b);
        }
    }
    public void make(int[] arr,int l,int r,int x){
        if (l==r){
            this.Arr[x] = arr[l];
        }
        else{
            int mid = (l+r)/2;
            make(arr, l, mid, (2*x)+1);
            make(arr, mid+1, r, (2*x)+2);
            this.Arr[x] = gcd(this.Arr[(2*x)+1],this.Arr[(2*x)+2]);
        }
    }
    public int query(int l,int r,int x,int L,int R){
        if (l>R || r<L){
            return 0;
        }
        if (L<=l && R>=r){
            return this.Arr[x];
        }
        int mid = (l+r)/2;
        return gcd(query(l,mid, (2*x)+1, L, R),query(mid+1, r, 2*x+2, L, R));
    }
}
class Comp implements Comparator<Point>{
    public int compare(Point a,Point b){
        return (a.x-b.x);
    } 
}
class dsu{
    int[] arr;
    dsu(int N){
        arr = new int[N];
        for (int i=0;i<N;i++){
            arr[i] = i;
        }
    }
    public void joint(int A,int B){
        int a = find(A);
        int b = find(B);
        arr[a] = b;
    }
    public int find(int A){
        while(A!=arr[A]){
            A = arr[A];
        }
        return A;
    }
}
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}