#include <bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define endl '\n'
#define ll long long
const int N = 50 + 5;
vector<ll> node[N];
bool vis[N];
ll a[N], b[N];
set<ll> s;
bool dfs(ll u, ll v){
        bool checker = false;
        if (u == v){
                return true;
        }
        if (u < v) {
                return false;
        }
        for (auto j : node[u]){
                checker = checker | (dfs(j, v));
        }
        return checker;
}
int main(){
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        //freopen("test.txt", "r", stdin);
        ll n;
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i];
        for (int j = 1; j <= n; j++) cin >> b[j];
        s.insert(51);
        bool flag = true;
        ll cur = 50;
        while (true){
                flag = true;
                for (int i = 0; i <= 50; i++) node[i].clear();
                for (int i = 1; i <= cur; i++){
                        for (int j = i; j <= 50; j++){
                                node[j].push_back(j % i);
                        }
                }
                for (auto j : s){
                        if (j == 51) continue;
                        for (int i = j; i <= 50; i++){
                                node[i].push_back(i % j);
                        }
                }
                for (int i = 1; i <= n; i++){
                        if (a[i] == b[i]) continue;
                        flag = flag & dfs(a[i], b[i]);
                }
                if (flag == false){
                        if ((cur + 1) == *s.begin()) {
                                cout << -1;
                                return 0;
                        }
                        else {
                                s.insert(cur + 1);
                        }
                } else {
                        cur--;
                        if (cur == 0) {
                                flag = true;
                                for (int i = 0; i <= 50; i++) node[i].clear();
                                for (auto j : s){
                                        if (j == 51) continue;
                                        for (int i = j; i <= 50; i++){
                                                node[i].push_back(i % j);
                                        }
                                }
                                for (int i = 1; i <= n; i++){
                                        if (a[i] == b[i]) continue;
                                        flag = flag & dfs(a[i], b[i]);
                                }
                                long long ans = 0;
                                for (auto j : s){
                                        if (j == 51) continue;
                                        ans += (1ll << j);
                                }
                                if (flag) {
                                        cout << ans;
                                        return 0;
                                } else {
                                        cout << ans + 2;
                                        return 0;
                                }
                        }
                }
        }
}


Measured memory_bytes: 256