#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define rrep(i,a,b) for(int i = (b); i --> (a);)
#define all(v) (v).begin(),(v).end()
#define trav(x,v) for(auto &x : v)
#define sz(v) int(v.size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;

int main(){
	int n;
	cin >> n;
	vi a(n), b(n);
	trav(x, a) cin >> x;
	trav(x, b) cin >> x;
	vector<bool> allowed(55, false);
	auto works = [&](){
		vi ls;
		rrep(i,0,55) if(allowed[i]) ls.push_back(i);
		rep(i,0,n){
			vector<bool> possible(51, false);
			possible[a[i]] = true;
			trav(k, ls){
				rep(j,0,51) if(possible[j])
					possible[j%k] = true;
			}
			if(!possible[b[i]]){
				return false;
			}
		}
		return true;
	};
	rep(k,1,55){
		if(k>1) allowed[k-1] = true;
		if(works()){
			rrep(i,0,k){
				allowed[i] = false;
				if(!works()) allowed[i] = true;
			}
			ll ans = 0;
			rep(i,0,55) if(allowed[i]) ans += 1LL<<i;
			cout << ans << endl;
			return 0;
		}
	}
	cout << -1 << endl;
}