#include <bits/stdc++.h>

//#define DEBUG 1

#define int long long
#define for0(i,n) for (int i=0; i<n; i++)
#define iter(c) for(auto it=c.begin(); it!=c.end(); it++)
#define iter2(c) for(auto it2=c.begin(); it2!=c.end(); it2++)
#define pb push_back
#define ms(a,z) memset(a,z,sizeof(a));
#define mp make_pair
#define X first
#define Y second
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define all(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
#define pmod(x,m) (((x)%(m)+m)%m)
#ifdef int
#define read(x) scanf("%lld",&x);
#else
#define read(x) scanf("%d",&x);
#endif

#ifdef DEBUG
#define nl cout<<"\n";
#define pr(x) cout<<(x)<<" ";
#define prl(x) cout<<#x " = "<<x<<endl;
#define prp(x) cout<<"("<<(x).first<<" "<<(x).second<<") ";
#define printv(v) {for(int _=0; _<sz(v); _++) cout<<v[_]<<" "; cout<<"\n";}
#define printa(a,s) {for (int _=0; _<s; _++) cout<<a[_]<<" "; cout<<"\n";}
#define print2D(a,m,n) {for (int _=0; _<m; _++) {for (int __=0; __<n; __++) cout<<a[_][__]<<" "; cout<<"\n";} cout<<"\n";}
#define priter(v) iter(v) {pr(*it)} nl
#define debug cout<<"ok at line "<<__LINE__<<endl;
#else
#define nl
#define pr(x)
#define prl(x)
#define prp(x)
#define printv(v)
#define printa(a,s)
#define print2D(a,m,n)
#define priter(v)
#define debug
#endif
#define MAXN 100000

using namespace std;

typedef long long ll;

const int INF = 2147483647;
const long long INFL = 9223372036854775807LL;
const double EPSILON = 0.00000001;
const long long MOD = 1000000007;

int max_mod[55][55];
//ll cost[55][55];

int32_t main()
{
    #ifdef DEBUG
    //freopen("C.txt","r",stdin);
    //freopen("","w",stdout);
    #endif

    ms(max_mod,-1);
    //ms(cost,0);
    for (int a=0; a<=50; a++) {
        max_mod[a][a] = 0;
        for (int b=0; b<a; b++) {
            for (int m=1; m<=a; m++) {
                if (max_mod[a%m][b] != -1) {
                    max_mod[a][b] = m;
                    //cost[a][b] = (1LL<<m) + cost[a%m][b];
                    break;
                }
            }
        }
    }
    //print2D(max_mod,20,20);
    //print2D(cost,10,10);
    
    int n;
    cin >> n;
    vector<int> A(n), B(n);
    for0(i,n) 
        cin >> A[i];
    for0(i,n)
        cin >> B[i];
        
    for (int i=0; i<n; i++) {
        if (max_mod[A[i]][B[i]] == -1) {
            cout<<-1<<endl;
            return 0;
        }
    }
    
    vector<set<int>> vals;
    for0(i,n) {
        set<int> v;
        v.insert(A[i]);
        vals.pb(v);
    }
    
    ll ans = 0;
    while (true) {
        int m = 0;
        for (int i=0; i<n; i++) {
            int M = INF;
            for (int x : vals[i]) {
                M = min(M,max_mod[x][B[i]]);
            }
            m = max(m,M);
        }
        prl(m)
        if (m == 0)
            break;
        
        ans += 1LL<<m;
        for (int i=0; i<n; i++) {
            vector<int> new_vals;
            for (int x : vals[i]) {
                if (max_mod[x%m][B[i]] != -1) {
                    new_vals.pb(x%m);
                }
            }
            for (int x : new_vals)
                vals[i].insert(x);
        }
        nl
    }
    
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256