#include<iostream>
#include<cstring>
using namespace std;
long long n,a[55],b[55],ans,dp[55][55];
bool f[55],flag;
int main()
{
	cin>>n;
	for (long long i=1;i<=n;i++) cin>>a[i];
	for (long long i=1;i<=n;i++) cin>>b[i];
	for (long long h=1;h<=50;h++)
	{
		memset(dp,0,sizeof(dp));
		dp[51][a[h]]=1;
		for (long long j=50;j>=1;j--)
		{
			for (long long k=0;k<=50;k++)
			{
				if (dp[j+1][k])
				{
					dp[j][k]=1;
					dp[j][k%j]=1;
				}
			}
		}
		if (!dp[1][b[h]])
		{
			flag=1;
			break;
		}
	}
	if (flag) 
	{
		cout<<-1<<endl;
		return 0;
	}
	for (long long i=50;i>=1;i--)
	{
		flag=0;
		for (long long h=1;h<=50;h++)
		{
			memset(dp,0,sizeof(dp));
			dp[51][a[h]]=1;
			for (long long j=50;j>=1;j--)
			{
				if (j>=i && !f[j])
				{
					for (long long k=0;k<=50;k++) dp[j][k]=dp[j+1][k];
			 		continue;
				}
				for (long long k=0;k<=50;k++)
				{
					if (dp[j+1][k])
					{
						dp[j][k]=1;
						dp[j][k%j]=1;
					}
				}
			}
			if (!dp[1][b[h]])
			{
				flag=1;
				break;
			}
		}
		if (flag)
		{
			ans+=(1ll<<i);
			f[i]=1;
		}
	}
	cout<<ans<<endl;
	return 0;
}