// clang-format off
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#ifndef ONLINE_JUDGE
// #define sarthak
#endif

#ifndef sarthak
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
        tree_order_statistics_node_update> oset;
// not that imp
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef complex<ld> cd;

#define INF 4557430888798830399ll
#define MOD 1000000007
#define EPS 1e-7
#define PI 3.1415926535897932385
// #define PI acos(-1)

#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define ff first
#define ss second
#define all(ar) ar.begin(), ar.end()
#define sz(x) (int)(x).size()
#define len(x) (int)x.length()

#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar))  // 0x3f for inf, 0x80 for -INF can also use with pairs
#define trav(a, x) for(auto& a : x)
// #define rep(i, a, b) for(int i = a; i < (b); ++i)  // For kactl
// #define FOR(i, a, b) for (int i = (a); i < (b); i++)  // For Benq
// #define F0R(i, a) for (int i = 0; i < (a); i++)  // For Benq
// #define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)  // For Benq
// #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)  // For Benq

#ifdef sarthak
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    int bracket=0,i=0;
    for(; ;i++)
        if(names[i]==','&&bracket==0)
            break;
        else if(names[i]=='(')
            bracket++;
        else if(names[i]==')')
            bracket--;
    const char *comma=names+i;
    cout.write(names,comma-names)<<" : ";
    __p(arg1);
    cout<<"| ";
    __f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif

mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
    ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }
// clang-format on

const int maxn = 1000005;
    
const ll N = 1000000;
void solve() 
{
    ll n;
    cin >> n;

    ll a[n+1];
    ll b[n+1];

    fr(i,1,n)
    {
        cin >> a[i];
    }
    fr(i,1,n)
    {
        cin >> b[i];
    }

    ll co = 0;
    ll po[51];
    po[0] = 1;

    fr(i,1,50)
    {
        po[i] = po[i-1]*2;
    }

    ll dp[51][51];
    fil(dp,0);

    fr(i,0,50)
    {
        fr(j,0,50)
        {
            dp[i][j] = INF;
        }
        dp[i][i] = 0;
    }

    frr(i,50,1)
    {
        ll dp2[51][51];
        fr(j,0,50)
        {
            fr(k,0,50)
            {
                dp2[j][k] = dp[j][k];
            }
        }        

        frr(j,i-1,0)
        {
            fr(k,0,50)
            {
                if(j!=0)
                    dp2[k][k%j] = min(dp2[k][k%j],1ll);
            }
        }

        fr(j,0,50)
        {
            fr(k,0,50)
            {
                fr(l,0,50)
                {
                    dp2[k][l] = min(dp2[k][l],dp2[k][j]+dp2[j][l]);
                }
            }
        }

        bool take = 0;
        fr(j,1,n)
        {
            if(dp2[a[j]][b[j]]>=INF)
            {
                take = 1;
            }
        }

        if(take)
        {
            // trace(co,i);
            co = co + po[i];
            fr(j,0,50)
            {
                dp[j][j%i] = min(dp[j][j%i],1ll);
            }
        }
    }

    fr(j,0,50)
    {
        fr(k,0,50)
        {
            fr(l,0,50)
            {
                dp[k][l] = min(dp[k][l],dp[k][j]+dp[j][l]);
            }
        }
    }

    bool take = 0;
    fr(j,1,n)
    {
        if(dp[a[j]][b[j]]>=INF)
        {
            take = 1;
        }
    }

    if(take)
        cout << -1 << endl;
    else
        cout << co << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(10);  // cout<<fixed;
#ifdef sarthak
    // cin.exceptions(cin.failbit);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t=1;
    // cin>>t; 
    fr(i,1,t){
        // cout<<"Case #"<<i<<": ";
        solve();
    }
    end_routine();
}


Measured memory_bytes: 256