#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){
	ll a=0,b=getchar(),c=1;
	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();
	while(isdigit(b))a=a*10+b-'0',b=getchar();
	return a*c;
}
int n,a[55],b[55];
bool gg(ll x,int y,int z){
    if(z>y)return false;
    vector<int> dp(y+1,0);
    dp[y]=1;
    for(int i=49;i>=0;i--){
        if(!(x&(1LL<<i)))continue;
        for(int j=0;j<=y;j++){
            if(!dp[j])continue;
            dp[j%(i+1)]=1;
        }
    }
    return dp[z];
}
bool jj(ll x){
    for(int i=0;i<n;i++)
		if(!gg(x,a[i],b[i]))
			return false;
    return true;
}
int main(){
	n=read();
	for(int i=0;i<n;i++)
		a[i]=read();
	for(int i=0;i<n;i++)
		b[i]=read();
    ll ans=(1LL<<50)-1;
    if(!jj(ans))
    	return puts("-1"),0;
    for(int i=49;i>=0;i--){
        ans^=(1LL<<i);
        if(!jj(ans))ans|=(1LL<<i);
    }
    cout << 2*ans;
	return 0;
}