using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
static class Program{
	const int mod=(int)1e9+7;
		static void Main(){
		Sc sc=new Sc();
		int n=sc.I;
		int[] a=sc.Ia;
		int[] b=sc.Ia;
		int max=a.Max();
		bool[] bo=new bool[n];
		int[,] h=new int[max+1,max+1];
		for(int i = 1;i<=max;i++) {
			for(int j = 1;j<=i;j++) {h[i,j]=i%j;}
		}
		long ans=0,k=0;
		while(k<n&&ans!=-1){
			long o=0,p=0;
			for(int i = 0;i<n;i++) {
				if(a[i]==b[i]||bo[i]){
					if(!bo[i]){k++;bo[i]=true;}
					continue;
				}
				var pq=new Pq<Mo<int>>(n,true);
				for(int j = 1;j<=a[i];j++){
					if(h[a[i],j]>=b[i]){pq.Push(new Mo<int>((ans&(1L<<j))==0?(1L<<j):0,h[a[i],j]));}
				}
				while(pq.cnt>0){
					Mo<int> q=pq.Top;
					if(q.d==b[i]){break;}
					pq.Pop();
					for(int j = 1;j<=q.d;j++){
						if(h[q.d,j]>=b[i]){pq.Push(new Mo<int>(q.n+((ans&(1L<<j))==0?(1L<<j):0),h[q.d,j]));}
					}
				}
				if(pq.cnt==0){
					ans=-1;
					break;
				}
				else if(p<pq.Top.n){
					p=pq.Top.n;
					o=i;
				}
			}
			ans|=p;
			bo[o]=true;
			k++;
		}
		Console.WriteLine(ans);
	}
}
public class Mo<T>:IComparable{
	public long n;
	public T d;
	public Mo(long n,T d){this.n=n;this.d=d;}
	public int CompareTo(object obj){
		Mo<T> mymo=(Mo<T>)obj;
		if(mymo.n>n){return -1;}
		else if(mymo.n<n){return 1;}
		else{return 0;}
	}
}
public class Pq<T> where T:IComparable{
	private T[] he;
	public int cnt=0,max=0;
	private Func<T,T,int> compare;
	public Pq(int max,bool mm){
		this.max=max;
		he=new T[max];
		if(mm){compare=Ao;}
		else{compare=Do;}
	}
	public void Push(T x){
		if(cnt==max){Extend();}
		int j=cnt;
		while(j!=0&&compare(x,he[(j-1)>>1])>0){he[j]=he[(j-1)>>1];j=(j-1)>>1;}
		he[j]=x;
		cnt++;
	}
	public void Pop(){
		cnt--;
		T r=he[cnt];
		int j=0;
		while(true){
			if(j*2+1<cnt){
				if(compare(he[j*2+1],he[j*2+2])>0){j=j*2+1;}
				else{j=j*2+2;}
			}
			else if(j*2<cnt){j=j*2+1;}
			else{break;}
			if(compare(he[j],r)<=0){j=(j-1)>>1;break;}
			he[(j-1)>>1]=he[j];
		}
		he[j]=r;
	}
	private int Ao(T x,T y){return y.CompareTo(x);}
	private int Do(T x,T y){return x.CompareTo(y);}
	public T Top{get{return he[0];}}
	private void Extend(){
		T[] nhe=new T[max<<1];
		Array.Copy(he,nhe,max);
		he=nhe;
		max<<=1;
	}
}

public class Sc{
	private string[] a;
	private int n=-1,m=0;
	public int I{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return int.Parse(a[n]);}}
	public long L{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return long.Parse(a[n]);}}
	public double D{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return double.Parse(a[n]);}}
	public string S{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return a[n];}}
	public int[] Ia{get{if(n!=-1&&n!=a.Length-1){m=n;n=-1;return Array.ConvertAll(a.Skip(m+1).ToArray(),int.Parse);}return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()).Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
}

Measured memory_bytes: 15424