//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(int)n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

#define N 51

//head

int n;
int a[N], b[N];
int G[N][N];
vi lst[N];
set<int> start_list[N];
bitset<N> check;
vi ans;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, 51) rep(j, 50) G[i][j+1] = i%(j+1);
  rep(i, 51) {
    lst[i].emplace_back(i);
    for(int j = 1; j <= i; j++) {
      rep(k, lst[G[i][j]].size()) lst[i].emplace_back(lst[G[i][j]][k]);
    }
    sort(all(lst[i]));
    lst[i].erase(unique(all(lst[i])), lst[i].end());
  }
  //rep(i, 51) rep(j, lst[i].size()) cout << lst[i][j] << (j == lst[i].size()-1?'\n':' ');
  rep(i, n) start_list[i].emplace(a[i]);
  int ma = -1;
  rep(i, n) {
    if(a[i] == b[i]) {
      check.set(i);
      continue;
    }
    auto itr = lower_bound(all(lst[a[i]]), b[i]);
    if(itr == lst[a[i]].end() or *itr != b[i]) {
      cout << -1 << endl;
      return 0;
    }
    for(int j = 1; j <= a[i]; j++) {
      int u = G[a[i]][j];
      auto itr = lower_bound(all(lst[u]), b[i]);
      if(itr != lst[u].end() and *itr == b[i]) {
        //cout << P(a[i], b[i]) << ' ' << P(j, u) << endl;
        chmax(ma, j);
        break;
      }
    }
  }
  while(ma != -1) {
    ans.emplace_back(ma);
    rep(i, n) if(!check.test(i)) {
      vi uu;
      for(int x:start_list[i]) {
        if(G[x][ma] == b[i]) {
          check.set(i);
          break;
        }
        auto itr = lower_bound(all(lst[G[x][ma]]), b[i]);
        if(itr == lst[G[x][ma]].end() or *itr != b[i]) continue;
        uu.emplace_back(G[x][ma]);
      }
      rep(j, uu.size()) start_list[i].emplace(uu[j]);
    }
    int z = ma;
    ma = -1;
    rep(i, n) if(!check.test(i)) {
      int mi = INF;
      vi uu;
      for(int x:start_list[i]) {
        for(int j = 1; j <= x and j < z; j++) {
          auto itr = lower_bound(all(lst[G[x][j]]), b[i]);
          if(itr != lst[G[x][j]].end() and *itr == b[i]) {
            chmin(mi, j);
            break;
          }
          if(j == z-1) uu.emplace_back(x);
        }
      }
      rep(j, uu.size()) start_list[i].erase(uu[j]);
      chmax(ma, mi);
      //if(ma == INF) cout << a[i] << ' ' << b[i] << endl;
    }
  }
  ll r = 0;
  rep(i, ans.size()) r |= 1LL<<ans[i];
  cout << r << endl;
}

Measured memory_bytes: 3720