#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#define sqr(x) (x)*(x)
using namespace std;
long long n,m,i,j,dp[55][55],fr[55],to[55],ans;
bool check(long long x)
{
	long long i,j,k;
	memset(dp,0,sizeof(dp));
	for (i=1;i<=n;i++)
	{
		dp[i][fr[i]]=1;
		for (j=50;j>=1;j--)
		{
			if (!((1ll<<j)&x)) continue;
			for (k=j;k<=50;k++)
			{
				if (!dp[i][k]) continue;
				dp[i][k%j]=1;
			}
		}
		if (!dp[i][to[i]]) return 0;
	}
	return 1;
}
int main()
{
	cin>>n;
	for (i=1;i<=n;i++)
	{
		cin>>fr[i];
	}
	for (i=1;i<=n;i++)
	{
		cin>>to[i];
	}
	for (i=1;i<=n;i++)
	{
		if (to[i]!=fr[i]&&to[i]*2>=fr[i])
		{
			cout<<-1;
			return 0;
		}
	}
	for (i=1;i<=50;i++) ans|=(1ll<<i);
	for (i=50;i;i--)
	{
		if (ans>=(1ll<<i)&&check(ans-(1ll<<i))) ans-=(1ll<<i);
	}
	cout<<ans;
	return 0;
}