#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n; cin >> n;
    vector<int> a(n);
    for (auto &ai: a) cin >> ai;
    vector<int> b(n);
    for (auto &bi: b) cin >> bi;
    auto check = [&](long long s) {
        for (int i = 0; i < n; i++) {
            vector<int> dp(50 + 1);
            dp[a[i]] = true;
            for (int k = 50; k >= 1; k--) if (s >> k & 1) {
                for (int x = 1; x <= 50; x++) dp[x % k] |= dp[x];
            }
            if (!dp[b[i]]) return false;
        }
        return true;
    };
    long long ans = (1LL << 51) - 1 - (1LL << 0);
    if (!check(ans)) return !(cout << -1 << endl);
    for (int k = 50; k >= 1; k--) {
        ans ^= 1LL << k;
        if (!check(ans)) ans ^= 1LL << k;
    }
    cout << ans << endl;
    return 0;
}
