N = int(input())
X = list(map(int, input().split()))
F = list(map(int, input().split()))
# print(N)
class node:
def __init__(self):
self.ch = [None, None]
self.num = [] # 记录当前节点的所有相关节点,方便后续判断是否相邻
class dictTree:
BitSize = 31 # 32位整数
def __init__(self, num, X):
self.root = node()
self.X = X
ze,on = self.split(num, self.BitSize) # 树的第二层预先划分
self.lazyInsert(ze,on,self.root)
# 按第n位划分列表
def split(self, num, n):
ze = [] # 0
on = [] # 1
for i in num:
val = self.X[i]
if (val >> n) & 1:
on.append(i)
else:
ze.append(i)
return ze, on
# 懒插入操作,直接全部插入超时
def lazyInsert(self, ze, on, nd):
nd.ch[0] = node()
nd.ch[1] = node()
nd.ch[0].num = ze
nd.ch[1].num = on
# 插入字典树
# def insert(self, value, n):
# nd = self.root
# for i in range(self.BitSize, -1, -1):
# bit = (value >> i) & 1
# if nd.ch[bit] is None:
# nd.ch[bit] = node()
#
# nd = nd.ch[bit]
# 检查最优的节点是否有非相邻点
def __check(self, nd, num, F):
for i in num:
if F[i] != nd and F[nd] != i:
return True
return False
#计算最大异或值
def maxXor(self, value, n, F, maxAns):
nd = self.root
ans = 0
ismax = False
for i in range(self.BitSize, -1, -1):
bit = (value >> i) & 1
bit ^= 1
if nd.ch[bit] is None:
ze, on = self.split(nd.num, i)
self.lazyInsert(ze, on, nd)
ison = 1 # 判断第i位异或是否为1
if self.__check(n, nd.ch[bit].num, F):
ans ^= 1 << i
else:
ison = 0
bit ^= 1
# 剪枝,判断能否有机会超过目前最大值
if not ismax:
maxbit = (maxAns >> i) & 1
if maxbit < ison:
ismax = True
elif maxbit > ison:
# print('maxbit:',maxbit,'bit:',bit)
# print('i:',i)
# print("ans:",ans)
# print("maxAns:",maxAns)
return maxAns
nd = nd.ch[bit]
# print("ansre:",ans,'value:',value,'n:',n)
return ans
dictTr = dictTree(range(N), X)
ans = 0
# 原始插入方法
# for i in range(N):
# dictTr.insert(X[i], i)
for i in range(N):
ans = dictTr.maxXor(X[i], i, F, ans)
print(ans)