#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>
#include <functional>
#include <set>
#include <map>
#include <random>
/* All our base are belong to AC */
#define DB cerr<<"D"<<endl
using namespace std; using ll=long long; using ld=long double; const int INF=1e9; const ll LINF=1e18; const double dINF = 1e18; const ld ldINF = 1e18; const double EPS = 1e-6;
template<typename T, typename U, typename O> void caut(T a, U b, O c){cout<<"("<<a<<","<<b<<","<<c<<") ";} template<typename T, typename U> void caut(T a, U b){cout<<"("<<a<<","<<b<<") ";} template<typename T> void caut(T a){cout<<"("<<a<<") ";};
using P=pair<int,int>;
const ll M = 1e9+7;
ll mod_pow(ll x, ll a) { ll an = 1; while(a > 0) { if (a&1) an = an * x % M; x = x * x % M; a >>= 1;} return an;}
ll adad(ll x) {return x*(x+1)/2;} // 1～xの総和
void add(ll& x, ll y) {x+=y; x%=M;}; void mul(ll& x, ll y) {x*=y; x%=M;}; template<typename T, typename U> void chmax(T& x, U y) {if (x<y) x=y;}; template<typename T, typename U> void chmin(T& x, U y) {if (x>y) x=y;};
bool vaild(int x, int y, int hh, int ww){return 0<=x&&x<hh&&0<=y&&y<ww;}

set<ll> p[51][51];
void gt(ll fr, ll nw, ll cs) {
  for(ll i=1;i<=50;i++) {
    if (nw % i == nw) continue;
    else {
      //if (fr == 10 && nw % i == 3) cout<<cs<<endl;
      ll cs2 = cs | 1LL<<i;
      p[fr][nw%i].insert(cs2);
      gt(fr, nw%i, cs2);
    }
  }
}
void init() {
  for(int i=0;i<=50;i++) {
    p[i][i].insert(0);
    gt(i, i, 0);
  }
  
  
  /*for(int i=0;i<=50;i++) {
    for(int j=0;j<=50;j++) {
      if (p[i][j].size() == 0) continue;
      cout<<i<<" - "<<j<<" : "<<p[i][j].size()<<endl;
      //for(ll x : p[i][j]) {
      //  cout<<x<<" ";
      //}
    } cout<<endl;
  }*/
  /*
  cout<<19<<" - "<<0<<" : "<<endl;
  for(ll x : p[19][0]) {
    cout<<x<<" ";
  } cout<<endl;
  cout<<10<<" - "<<3<<" : "<<endl;
  for(ll x : p[10][3]) {
    cout<<x<<" ";
  } cout<<endl;
  cout<<14<<" - "<<4<<" : "<<endl;
  for(ll x : p[14][4]) {
    cout<<x<<" ";
  } cout<<endl;*/
  // p[i][j] = {iからjを作る手段} 0010011101
}

int main() {
  init();
  int n; cin>>n;
  int a[n],b[n];
  for(int i=0;i<n;i++) cin>>a[i];
  for(int i=0;i<n;i++) cin>>b[i];

  bool ok = true;
  for(int i=0;i<n;i++) {
    ok &= a[i] == b[i];
  }
  if (ok) {
    cout<<0<<endl;
    return 0;
  }

  ok = true;
  for(int i=0;i<n;i++) {
    ok &= !p[a[i]][b[i]].empty();
  }
  if (!ok) {
    cout<<-1<<endl;
    return 0;
  }

  ll u = 1;
  while(true) {
    bool pos = true;
    for(int i=0;i<n;i++) {
      bool get = false;
      for(ll x : p[a[i]][b[i]]) {
        if (u >= x) {
          //cout<<i<<" "<<a[i]<<" "<<b[i]<<" "<<u<<" "<<x<<endl;
          get = true; 
          break;
        }
      }
      pos &= get;
    }
    if (pos) {
      break;
    } else {
      u<<=1; u++;
    }
  }
  //cout<<u<<endl;

  for(ll bt=1LL<<50LL; bt>0; bt>>=1) {
    if (!(u & bt)) continue;
    ll u2 = u - bt;
    bool pos = true;
    for(int i=0;i<n;i++) {
      bool get = false;
      for(ll x : p[a[i]][b[i]]) {
        if ((x & u2) == x) {
          //cout<<bt<<" "<<i<<" "<<a[i]<<" "<<b[i]<<" "<<u<<" "<<x<<endl;
          get = true;
          break;
        }
      }
      pos &= get;
    }
    if (pos) {
      u = u2;
    }
  }
  cout<<u<<endl;
}

Measured memory_bytes: 4944