Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.

There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).

She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. 

If this is possible, calculate the minimal cost.

Input

The first line contains an integer n (1 ≤ n ≤ 300), number of cards.

The second line contains n numbers li (1 ≤ li ≤ 109), the jump lengths of cards.

The third line contains n numbers ci (1 ≤ ci ≤ 105), the costs of cards.

Output

If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.

Examples

Input

3
100 99 9900
1 1 1


Output

2


Input

5
10 20 30 40 50
1 1 1 1 1


Output

-1


Input

7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10


Output

6


Input

8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026


Output

7237

Note

In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.

In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
/*Talent is something you make bloom , instinct is something you polish*/
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
    static long mod=((long)1e9)+7;//toString
    public static int gcd(int a,int b){if(b==0)return a;else return gcd(b,a%b);}
    public static long pow_mod(long x,long y){long res=1;x=x%mod;while(y > 0){if((y & 1)==1)res=(res * x)%mod;y=y>>1;x =(x * x)%mod;}return res;}
    public static int lower_bound(int[]arr,int val){int lo=0;int hi=arr.length-1;while(lo<hi){int mid=lo+((hi-lo+1)/2);if(arr[mid]==val){return mid;}else if(arr[mid]>val){hi=mid-1;}else lo=mid;}if(arr[lo]<=val)return lo;else return -1;}
    public static int upper_bound(int[]arr,int val){int lo=0;int hi=arr.length-1;while(lo<hi){int mid=lo+((hi-lo)/2);if(arr[mid]==val){return mid;}else if(arr[mid]>val){hi=mid;;}else lo=mid+1;}if(arr[lo]>=val)return lo;else return -1;}
    
    public static void main (String[] args) throws java.lang.Exception
    {
        Reader sn = new Reader();
        Print p = new Print();
        int n = sn.nextInt();
        ArrayList<Integer> asl = new ArrayList<Integer>();
        int[] lengths = new int[n];
        int[] costs = new int[n];
        for(int i = 0; i < n; ++i){
            lengths[i] = sn.nextInt();
        }
        for(int i = 0; i < n; ++i){
            costs[i] = sn.nextInt();
        }
        int[] arr = new int[((int)1e7) + 1];
        int index = 0;
        int start = 0;
        int end = 0;
        HashMap<Integer , Integer> hm1 = new HashMap<Integer , Integer>();
        TreeSet<Integer> ts = new TreeSet<Integer>();
        hm1.put(lengths[0] ,index);
        index++;
        arr[hm1.get(lengths[0])] = costs[0];
        ts.add(lengths[0]);
        for(int i = 1; i < n; ++i){
            if(hm1.get(lengths[i]) != null){
                arr[hm1.get(lengths[i])] = Math.min(arr[hm1.get(lengths[i])] , costs[i]);
            }
            else{
                hm1.put(lengths[i] , index);
                ts.add(lengths[i]);
                arr[hm1.get(lengths[i])] = costs[i];
                index++;
            }
            Iterator<Integer> it = ts.iterator();
            while(it.hasNext()){
                
                int abc = it.next();
                int g = gcd(abc , lengths[i]);
                if(hm1.get(g) != null){
                    arr[hm1.get(g)] = Math.min(arr[hm1.get(g)] , (costs[i] + arr[hm1.get(abc)]));
                }
                else{
                    asl.add(g);
                    hm1.put(g , index);
                    index++;
                    arr[hm1.get(g)] = costs[i] + arr[hm1.get(abc)];
                    
                }
            }
            for(int j = 0; j < asl.size(); ++j){
                ts.add(asl.get(j));
            }
        }
        if(hm1.get(1) == null){
            p.printLine("-1");
        }
        else{
            p.printLine(Integer.toString(arr[hm1.get(1)]));
        }
        p.close();
    }
}
class Pair implements Comparable<Pair> {
		int val;
		int in;
		Pair(int a, int b){
			val=a;
	        in=b;
		}
		@Override
		public int compareTo(Pair o) {
			if(val==o.val)
			return Integer.compare(in,o.in);
			else
			return Integer.compare(val,o.val);
		}}
class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;
        public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; }
        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
 
        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
 
        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }
        
        public String readWord()throws IOException
        {
		int c = read();
		while (isSpaceChar(c)) c = read();        
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		}while (!isSpaceChar(c));
		return res.toString();
	    } 
	
        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');
 
            if (neg)
                return -ret;
            return ret;
        }
 
        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }
 
        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
 
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
 
            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }
 
            if (neg)
                return -ret;
            return ret;
        }
 
        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }
 
        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }
 
        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
class Print
{
    private final BufferedWriter bw;
    public Print()
    {
        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public void print(String str)throws IOException
    {
        bw.append(str);
    }
    public void printLine(String str)throws IOException
    {
        print(str);
        bw.append("\n");
    }
    public void close()throws IOException
    {
        bw.close();
    }}