#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
set<long long>xp[60];
set<long long>::iterator it;
int n,cnt_list,m;
long long a[1<<18],b[1<<18],list[1<<18],ax,hs[2][1<<18];
long long ans;
bool dvd[1<<18];
int up;
bool dfs(int id)
{
if(id<0)
return true;
int i,j,s,p,q,pre;
bool can[2];
int num[2][2];
memset(can,true,sizeof(can));
pre=0;
for(j=0;j<n;j++)
{
if(dvd[j]||j==n-1)
{
memset(num,0,sizeof(num));
for(s=pre;s<=j;s++)
{
if(a[s]&(1LL<<id))
num[0][1]++;
else
num[0][0]++;
if(b[s]&(1LL<<id))
num[1][1]++;
else
num[1][0]++;
}
if(num[0][0]!=num[1][0])
can[0]=false;
if(num[0][0]!=num[1][1])
can[1]=false;
if(!can[0]&&!can[1])
return false;
pre=j+1;
}
}
for(i=0;i<=1;i++)
{
if(!can[i])
continue;
pre=0;
vector<int>vec;
vec.clear();
for(j=0;j<n;j++)
{
if(dvd[j]||j==n-1)
{
cnt_list=0;
for(s=pre;s<=j;s++)
{
if(!(a[s]&(1LL<<id)))
list[cnt_list++]=a[s];
}
for(s=pre;s<=j;s++)
{
if(a[s]&(1LL<<id))
list[cnt_list++]=a[s];
}
for(s=0;s<cnt_list;s++)
a[s+pre]=list[s];
cnt_list=0;
if(i==0)
{
for(s=pre;s<=j;s++)
{
if(!(b[s]&(1LL<<id)))
list[cnt_list++]=b[s];
}
for(s=pre;s<=j;s++)
{
if(b[s]&(1LL<<id))
list[cnt_list++]=b[s];
}
}
else
{
for(s=pre;s<=j;s++)
{
if(b[s]&(1LL<<id))
list[cnt_list++]=b[s];
}
for(s=pre;s<=j;s++)
{
if(!(b[s]&(1LL<<id)))
list[cnt_list++]=b[s];
}
}
for(s=0;s<cnt_list;s++)
b[s+pre]=list[s];
for(s=pre;s<=j;s++)
{
if(a[s]&(1LL<<id))
{
if(s>pre)
{
dvd[s-1]=true;
vec.push_back(s-1);
}
break;
}
}
pre=j+1;
}
}
if(i==1)
ans|=(1LL<<id);
it=xp[id].find(ans);
if(it!=xp[id].end())
{
if(dfs(id-1))
return true;
}
if(i==1)
ans^=(1LL<<id);
for(j=0;j<vec.size();j++)
dvd[vec[j]]=false;
}
return false;
}
int main()
{
int i,j,s,p,q,id;
freopen("simple.in","r",stdin);
freopen("simple.out","w",stdout);
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%I64d",&a[i]);
for(i=0;i<n;i++)
scanf("%I64d",&b[i]);
// n=1<<18;
// for(i=0;i<n;i++)
//{
// a[i]=0;
// for(j=0;j<60;j++)
// a[i]=(a[i]<<1LL)+rand()%2;
// }
// long long c=0;
// for(j=0;j<60;j++)
// c=(c<<1LL)+rand()%2;
// for(i=0;i<n;i++)
// b[i]=a[i]^c;
ax=0;
for(i=0;i<n;i++)
{
ax=max(ax,a[i]);
ax=max(ax,b[i]);
}
random_shuffle(a,a+n);
for(up=0;(1LL<<up)<=ax;up++);
for(i=0;i<min(n,9);i++)
{
for(j=0;j<n;j++)
{
if(i==0)
hs[0][j]=a[i]^b[j];
else
hs[1][j]=a[i]^b[j];
}
if(i==0)
{
sort(hs[0],hs[0]+n);
m=n;
}
else
{
sort(hs[1],hs[1]+n);
cnt_list=0;
p=0;
q=0;
while(p<m&&q<n)
{
if(hs[0][p]<hs[1][q])
p++;
else if(hs[0][p]>hs[1][q])
q++;
else
{
list[cnt_list++]=hs[0][p];
p++;
q++;
}
}
for(p=0;p<cnt_list;p++)
hs[0][p]=list[p];
m=cnt_list;
}
}
for(i=0;i<up;i++)
xp[i].clear();
cnt_list=0;
for(i=0;i<m;i++)
{
if(cnt_list==0||hs[0][cnt_list-1]<hs[0][i])
hs[0][cnt_list++]=hs[0][i];
}
m=cnt_list;
for(i=0;i<m;i++)
{
long long now=0;
for(j=up-1;j>=0;j--)
{
now|=(hs[0][i]&(1LL<<j));
xp[j].insert(now);
}
}
memset(dvd,false,sizeof(dvd));
ans=0;
if(!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AgICBpZihudW1bMF1bMF0hPW51bVsxXVswXSkKCSAgIAkgICAgICAgIGNhblswXT1mYWxzZTsKCSAgICAgICAgaWYobnVtWzBdWzBdIT1udW1bMV1bMV0pCgkgICAgICAgICAgICBjYW5bMV09ZmFsc2U7CiAgICAgICAgICAgIGlmKCFjYW5bMF0mJiFjYW5bMV0pCiAgICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgCSAgIAkgICAgcHJlPWorMTsKICAgCSAgIH0KICAgIH0KCWZvcihpPTA7aTw9MTtpKyspCiAgICB7CiAgICAJaWYoIWNhbltpXSkKICAgIAkgICBjb250aW51ZTsKICAgIAlwcmU9MDsKICAgIAl2ZWN0b3I8aW50PnZlYzsKCSAgICB2ZWMuY2xlYXIoKTsKICAgIAlmb3Ioaj0wO2o8bjtqKyspCiAgICAJewoJICAgIAlpZihkdmRbal18fGo9PW4tMSkKCSAgICAJewoJCQkgICAgY250X2xpc3Q9MDsKCQkJICAgIGZvcihzPXByZTtzPD1qO3MrKykKCQkJICAgIHsKICAgIAkJCQlpZighKGFbc10mKDFMTDw8aWQpKSkKICAgIAkJCQkgICBsaXN0W2NudF9saXN0KytdPWFbc107IAogICAgCQkJfQogICAgCQkJZm9yKHM9cHJlO3M8PWo7cysrKQogICAgCQkJewoJCQkgICAgCWlmKGFbc10mKDFMTDw8aWQpKQoJCQkgICAgCSAgICBsaXN0W2NudF9saXN0KytdPWFbc107CgkJCSAgICB9CgkJCSAgICBmb3Iocz0wO3M8Y250X2xpc3Q7cysrKQoJCQkgICAgICAgYVtzK3ByZV09bGlzdFtzXTsKICAgIAkJCWNudF9saXN0PTA7CiAgICAJCQlpZihpPT0wKQogICAgCQkJewogICAgCQkJCWZvcihzPXByZTtzPD1qO3MrKykKICAgIAkJCQl7CgkJCQkgICAgCWlmKCEoYltzXSYoMUxMPDxpZCkpKQoJCQkJICAgIAkgIGxpc3RbY250X2xpc3QrK109YltzXTsKCQkJCSAgICB9CgkJCQkgICAgZm9yKHM9cHJlO3M8PWo7cysrKQoJCQkJICAgIHsKICAgIAkJCQkJaWYoYltzXSYoMUxMPDxpZCkpCiAgICAJCQkJCSAgIGxpc3RbY250X2xpc3QrK109YltzXTsKICAgIAkJCQl9CgkJCSAgICB9CgkJCSAgICBlbHNlCgkJCSAgICB7CiAgICAJCQkJZm9yKHM9cHJlO3M8PWo7cysrKQogICAgCQkJCXsKCQkJCSAgICAJaWYoYltzXSYoMUxMPDxpZCkpCgkJCQkgICAgCSAgIGxpc3RbY250X2xpc3QrK109YltzXTsKCQkJCSAgICB9CgkJCQkgICAgZm9yKHM9cHJlO3M8PWo7cysrKQoJCQkJICAgIHsKICAgIAkJCQkJaWYoIShiW3NdJigxTEw8PGlkKSkpCiAgICAJCQkJICAgICAgIGxpc3RbY250X2xpc3QrK109YltzXTsKICAgIAkJCQl9CiAgICAJCQl9CiAgICAJCQlmb3Iocz0wO3M8Y250X2xpc3Q7cysrKQogICAgCQkJICAgIGJbcytwcmVdPWxpc3Rbc107CiAgICAJCQlmb3Iocz1wcmU7czw9ajtzKyspCiAgICAJCQl7CgkJCSAgICAJaWYoYVtzXSYoMUxMPDxpZCkpCgkJICAgIAkgICAgewoJCSAgICAJICAgIAlpZihzPnByZSkKCQkgICAgCSAgICAJewogICAgCSAgICAJCSAgICAJZHZkW3MtMV09dHJ1ZTsKCQkgICAgCSAgICAJICAgIHZlYy5wdXNoX2JhY2socy0xKTsKCQkJCQkJfQoJCQkJCQlicmVhazsKICAgIAkgICAgCQl9CgkJCSAgICB9CgkgICAgCQlwcmU9aisxOwoJICAgIAl9CgkgICAgfQoJICAgIGlmKGk9PTEpCgkgICAgICAgYW5zfD0oMUxMPDxpZCk7CiAgICAgICAgaXQ9eHBbaWRdLmZpbmQoYW5zKTsKICAgICAgICBpZihpdCE9eHBbaWRdLmVuZCgpKQogICAgICAgIHsKICAgICAgICAgICAgaWYoZGZzKGlkLTEpKQogICAgICAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgICB9CgkJaWYoaT09MSkKICAgICAgICAgICBhbnNePSgxTEw8PGlkKTsKICAgICAgICBmb3Ioaj0wO2o8dmVjLnNpemUoKTtqKyspCiAgICAgICAgICAgIGR2ZFt2ZWNbal1dPWZhbHNlOwogICAgfQogICAgcmV0dXJuIGZhbHNlOwp9CmludCBtYWluKCkKewoJaW50IGksaixzLHAscSxpZDsKCWZyZW9wZW4oInNpbXBsZS5pbiIsInIiLHN0ZGluKTsKCWZyZW9wZW4oInNpbXBsZS5vdXQiLCJ3IixzdGRvdXQpOwoJc2NhbmYoIiVkIiwmbik7Cglmb3IoaT0wO2k8bjtpKyspCgkgICAgc2NhbmYoIiVJNjRkIiwmYVtpXSk7CiAgICBmb3IoaT0wO2k8bjtpKyspCiAgICAgICAgc2NhbmYoIiVJNjRkIiwmYltpXSk7CiAvLyAgIG49MTw8MTg7CiAgIC8vIGZvcihpPTA7aTxuO2krKykKICAgIC8vewogICAgLy8JYVtpXT0wOwogICAvLyAgCWZvcihqPTA7ajw2MDtqKyspCiAgICAvLwkgICBhW2ldPShhW2ldPDwxTEwpK3JhbmQoKSUyOwogIC8vICB9CiAgIC8vIGxvbmcgbG9uZyBjPTA7CiAgIC8vIGZvcihqPTA7ajw2MDtqKyspCiAgIC8vIGM9KGM8PDFMTCkrcmFuZCgpJTI7CiAgIC8vIGZvcihpPTA7aTxuO2krKykKICAgIC8vICAgYltpXT1hW2ldXmM7CglheD0wOwogICAgZm9yKGk9MDtpPG47aSsrKQogICAgewogICAgICAgIGF4PW1heChheCxhW2ldKTsKICAgICAgICBheD1tYXgoYXgsYltpXSk7Cgl9CglyYW5kb21fc2h1ZmZsZShhLGErbik7Cglmb3IodXA9MDsoMUxMPDx1cCk8PWF4O3VwKyspOwogICAgZm9yKGk9MDtpPG1pbihuLDkpO2krKykKICAgIHsKICAgIAlmb3Ioaj0wO2o8bjtqKyspCiAgICAJewogICAgCQlpZihpPT0wKQoJICAgICAgICAJaHNbMF1bal09YVtpXV5iW2pdOwogICAgCSAgICBlbHNlCiAgICAJICAgICAgICBoc1sxXVtqXT1hW2ldXmJbal07CgkJfQoJCWlmKGk9PTApCgkJewoJCSAgIHNvcnQoaHNbMF0saHNbMF0rbik7CgkJICAgbT1uOwoJCX0KCQllbHNlCiAgICAgICAgewogICAgICAgIAlzb3J0KGhzWzFdLGhzWzFdK24pOwogICAgICAgIAljbnRfbGlzdD0wOwogICAgICAgIAlwPTA7CiAgICAgICAgCXE9MDsKICAgICAgICAJd2hpbGUocDxtJiZxPG4pCiAgICAgICAgCXsKCSAgICAgICAgCWlmKGhzWzBdW3BdPGhzWzFdW3FdKQoJICAgICAgICAJICAgcCsrOwogICAJICAgICAgICAgICAgZWxzZSBpZihoc1swXVtwXT5oc1sxXVtxXSkKICAgCSAgICAgICAgICAgICAgIHErKzsKICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIAlsaXN0W2NudF9saXN0KytdPWhzWzBdW3BdOwogICAgICAgICAgICAgICAgCXArKzsKICAgICAgICAgICAgICAgIAlxKys7CiAgICAgICAgICAgICAgICB9CgkgICAgICAgIH0KCSAgICAgICAgZm9yKHA9MDtwPGNudF9saXN0O3ArKykKCSAgICAgICAgICAgIGhzWzBdW3BdPWxpc3RbcF07CiAgICAgICAgICAgIG09Y250X2xpc3Q7CiAgICAgICAgfQogICAgfQogICAgZm9yKGk9MDtpPHVwO2krKykKICAgICAgICB4cFtpXS5jbGVhcigpOwogICAgY250X2xpc3Q9MDsKICAgIGZvcihpPTA7aTxtO2krKykKICAgIHsKICAgIAlpZihjbnRfbGlzdD09MHx8aHNbMF1bY250X2xpc3QtMV08aHNbMF1baV0pCiAgICAJICAgIGhzWzBdW2NudF9saXN0KytdPWhzWzBdW2ldOwogICAgfQogICAgbT1jbnRfbGlzdDsKICAgIGZvcihpPTA7aTxtO2krKykKICAgIHsKICAgIAlsb25nIGxvbmcgbm93PTA7CiAgICAJZm9yKGo9dXAtMTtqPj0wO2otLSkKICAgIAl7CgkgICAgCW5vd3w9KGhzWzBdW2ldJigxTEw8PGopKTsKCSAgICAJeHBbal0uaW5zZXJ0KG5vdyk7CgkgICAgfQogICAgfQoJbWVtc2V0KGR2ZCxmYWxzZSxzaXplb2YoZHZkKSk7CiAgICBhbnM9MDsKICAgIGlmKCFkZnModXAtMSkpCiAgICAgICBwdXRzKCItMSIpOwoJZWxzZQogICAgICAgcHJpbnRmKCIlSTY0ZFxuIixhbnMpOwoJcmV0dXJuIDA7Cn0=