#include <bits/stdc++.h>
//make_tuple emplace_back next_permutation push_back make_pair second first setprecision


#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif

using LL = long long;
constexpr LL LINF=334ll<<53;
constexpr int INF=15<<26;
constexpr LL  MOD=1E9+7;

namespace Problem{
using namespace std;
class Writer{
static ostringstream oss;
public:template<class T> void appendln(const vector<vector<T>> &ans,string spl=" ", string el="\n"){for(int i=0; i<(int)ans.size(); ++i){for(int j=0; j<(int)ans[i].size(); ++j){oss << ans[i][j];oss << (j==(int)ans[i].size()-1?el:spl);}}}
    template<class T> void appendln(const vector<T>& ans, string spl=" "){for(int i=0; i<(int)ans.size(); ++i){oss << ans[i] << (i==(int)ans.size()-1?"\n":spl);}}
    template<class T> void appendln(const T& answer){oss << answer << "\n";}
    template<class T> void append(const T& answer){oss << answer;}
    void appends(const string spl){oss.seekp((int)oss.tellp()-1);oss<<"\n";}
    template<class F, class... R>void appends(const string spl,const F& fi, const R&... rest){oss << fi << spl; appends(spl, rest...);}
    static void write(){cout << oss.str();}
    Writer(){static bool is_single =true;assert(is_single);is_single=false;};
};ostringstream Writer::oss;


class Solver{
public:
    LL n;
    vector<LL> a,b;
    vector<set<LL>> aa;
    vector<vector<vector<LL>>> dp;
    vector<int> ok;
    Solver(LL n):n(n),a(n),b(n),aa(n),dp (51,vector<vector<LL>>(51,vector<LL>(51,-1))),ok(n){};

    void solve(){
        LL ans=0;
        for(int i=0; i<n; ++i){
            cin >> a[i];
        }
        for(int i=0; i<n; ++i){
            cin >> b[i];
        }
        for(int i=0; i<=50; ++i){
            dp[0][i][i]=0;
        }
        for(int i=1; i<=50; ++i){
            for(int j=0; j<=50; ++j){
                for(int k=0; k<=j; ++k){
                    //j->k
                    dp[i][j][k] = dp[i-1][j][k];
                    if(dp[i][j][k]==-1){
                        if(dp[i-1][j%i][k]!=-1){
                            dp[i][j][k] = (1ll<<i)+dp[i-1][j%i][k];
                        }
                    }
                }
            }
        }
        for(int i=0; i<n; ++i){
            aa[i].insert(a[i]);
        }
        for(int i=50; i>=1; --i){
            if(!check(i-1)){
                DBG(i)
                ans+=(1ll<<i);
                for(int j=0; j<n; ++j){
                    if(aa[j].find(b[j])==aa[j].end()){
                        if(i==1 and b[j] !=0){
                            cout <<-1 <<"\n";
                            return;
                        }

                    }
                    /*else{
                        if(dp[i-1][a[j]%i][b[j]] != -1 and
                            dp[i-1][a[j]%i][b[j]]<=dp[i-1][a[j]][b[j]]){
                            a[j]%=i;
                        }
                    }*/
                    set<LL> tmp;
                    for(auto x:aa[j]){
                        tmp.insert(x%i);
                    }
                    for(auto x:tmp){
                        aa[j].insert(x);
                    }
                }
                DBG(aa)
            }
        }
        cout << ans <<"\n";

    }
    bool check(int x){
        for(int i=0; i<n; ++i){
            bool ok = false;
            for(auto y: aa[i]){
                if(dp[x][y][b[i]]>-1) ok = true;
            }
            if(!ok) return false;
        }
        return true;
    }
};
}

int main(){
    std::cin.tie(0);
    std::ios_base::sync_with_stdio(false);
    long long n=0;
    std::cin >> n;
    Problem::Solver sol(n);
    sol.solve();
    Problem::Writer::write();
    return 0;
}



Measured memory_bytes: 1408