#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  vector<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];
  vector<vector<Int> > c(n);
  for(Int i=0;i<n;i++){
    function<void(Int,Int,Int)> dfs=[&](Int x,Int k,Int v){
      //cout<<x<<" "<<k<<" "<<v<<endl;
      if(v==b[i]){
	c[i].emplace_back(x);
	return;
      }
      if(v<b[i]) return;
      if(v>=k&&v%k>=b[i]) dfs(x|(1LL<<k),k-1,v%k);
      if(k-1>b[i]) dfs(x,k-1,v);
    };
    dfs(0,60,a[i]);
    sort(c[i].begin(),c[i].end());
    c[i].erase(unique(c[i].begin(),c[i].end()),c[i].end());
    //for(Int j:c[i]) cout<<bitset<30>(j)<<endl;
  }
  
  for(Int i=0;i<n;i++){
    if(c[i].empty()){
      cout<<-1<<endl;
      return 0;
    }
  }
  
  Int ans=0;
  for(Int k=60;k>0;k--){
    Int ng=0;
    for(Int i=0;i<n;i++){
      Int ok=0;
      for(Int j:c[i]){
	Int tmp=((ans|j)>>k)<<k;
	ok|=ans==tmp;
      }
      ng|=!ok;
      if(ng) break;
    }
    ans|=ng<<k;
  }
  cout<<ans<<endl;
  return 0;
}
