#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

int n,a[55],b[55];
bool g[55][55][55],val[55][55];
ll ret;
int main() {
	scanf("%d",&n);
	rep(i,0,n) scanf("%d",a+i);
	rep(i,0,n) scanf("%d",b+i);
	rep(j,0,51) g[0][j][j]=1;
	rep(k,1,51) rep(i,0,51) rep(j,0,51) g[k][i][j]=g[k-1][i%k][j]|g[k-1][i][j];
	rep(i,0,n) val[i][a[i]]=1;
	bool valid=1;
	rep(i,0,n) if (!g[50][a[i]][b[i]]) valid=0;
	if (!valid) {
		puts("-1");
		return 0;
	}
	per(k,1,51) {
		bool valid=1;
		rep(i,0,n) {
			bool ff=0;
			rep(j,0,51) ff|=val[i][j]&&g[k-1][j][b[i]];
			if (ff==0) valid=0;
		}
		if (!valid) {
			ret+=1ll<<k;
			rep(i,0,n) rep(j,0,51) val[i][j%k]|=val[i][j];
		}
	}
	printf("%lld\n",ret);
}