#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
bool vis[105];
ll a[55],b[55];
bool bfs(ll s,ll e,vector<ll> &v){
    memset(vis,0,sizeof(vis));
    queue<ll> q;
    q.push(s);
    vis[s]=1;
    while(!q.empty()){
        ll u=q.front();q.pop();
        for(auto &ele:v){
            if(!vis[u%ele])vis[u%ele]=1,q.push(u%ele);
        }
    }
    return vis[e];
}
bool chk(ll n,vector<ll> &v){
    for(ll i=0;i<n;i++)if(!bfs(a[i],b[i],v))return 0;
    return 1;
}
int main(){
    cin.tie(NULL); cout.tie(NULL);
    ios_base:: sync_with_stdio(false);
    ll n;cin>>n;
    ll mx=0;for(ll i=0;i<n;i++)cin>>a[i],mx=max(mx,a[i]);
    for(ll i=0;i<n;i++)cin>>b[i];
    ll ans = 0;
    vector<ll> v;
    for(ll i=mx+1;i>0;i--){
        vector<ll> tmp;
        for(ll j=i-1;j>0;j--)tmp.push_back(j);
        for(auto &ele:v)tmp.push_back(ele);
        if(!chk(n,tmp)){
            if(i==mx+1)return cout<<"-1"  ,0 ;
            ans+=(1ll<<i);
            v.push_back(i);
        }
    }

    //for(auto &ele:v)cout<<ele<<" ";cout<<endl;
    cout<<ans;
}


