#include <cstring>
#include <cstdio>
#include <vector>
#define pb push_back
std::vector<int> adj[55]; 
int a[55], b[55], que[55]; 
bool vis[55], ans[55]; 
int main()
{
	// freopen("AGC022-C.in", "r", stdin); 
	int n; 
	scanf("%d", &n); 
	for (int i = 0; i < n; i++)
		scanf("%d", a + i); 
	for (int i = 0; i < n; i++)
		scanf("%d", b + i); 
	for (int i = 51; i; i--)
	{
		for (int j = 0; j <= 50; j++)
			adj[j].clear(); 
		for (int j = 1; j <= 50; j++)
		{
			if (j >= i && !ans[j])
				continue; 
			for (int x = 0; x <= 50; x++)
				adj[x].pb(x % j); 
		}
		bool f = true; 
		for (int j = 0; j < n; j++)
		{
			memset(vis, false, sizeof(vis)); 
			int he = 0, ta = 0; 
			que[ta++] = a[j]; 
			vis[a[j]] = true; 
			while (he < ta)
			{
				int x = que[he++]; 
				for (int y : adj[x])
				{
					if (!vis[y])
					{
						vis[y] = true; 
						que[ta++] = y; 
					}
				}
			}
			f &= vis[b[j]]; 
		}
		ans[i] = !f; 
	}
	if (ans[51])
		puts("-1"); 
	else
	{
		long long x = 0; 
		for (int i = 0; i <= 50; i++)
			x |= (long long)ans[i] << i; 
		printf("%lld\n", x);
	}
	return 0; 
}
