#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
#define N 55
bool lis[N][N],v[N][N];
int a[N],b[N],n;

bool renketsu(){
    lol(u,n){
	int x=a[u],y=b[u];
	if(x<y)return false;
	if(x==y)continue;
	bool ok[N]={0};
	ok[x]=1;
	for(int i=x;i>y;i--){
	    if(ok[i]){
		for(int j=0;j<N;j++){
		    ok[j]|=v[i][j];
		}
	    }
	}
	if(ok[y])continue;
	else return false;
    }
    return true;
}

int F(int lim){
    lol(i,N)lol(j,N)v[i][j]=lis[i][j];
    
    if(renketsu())return 0;
    
    for(int k=1;k<lim;k++){
	
	for(int i=1;i<N;i++){
	    v[i][i%k]=true;
	}
	
	if(renketsu())return k;
	
	if(k==N-1)return -1;
    }
    return 0;
}

int main(){
    cin>>n;
    lol(i,n)cin>>a[i];
    lol(i,n)cin>>b[i];
    ll ans=0;
    int bef=N;
    while(true){
	int k=F(bef);
	if(k==-1){
	    cout<<"-1"<<endl;
	    return 0;
	}
	if(k==0)break;
	ans+=((ll)1<<k);
	bef=k;
	
	for(int i=1;i<N;i++){
	    lis[i][i%k]=true;
	}
    }
    cout<<ans<<endl;
    return 0;
}
