N = int(input()) X = list(map(int,input().split())) A = list(map(int,input().split())) from collections import defaultdict inf = 10 ** 13 d = defaultdict(lambda : inf) d[A[0]] = A[0] for i in range(N - 1): x = X[i] xx = X[i + 1] a = A[i + 1] nx = defaultdict(lambda : inf) b = A[i] for aa,u in d.items(): nx[aa ^ a] = min(nx[aa ^ a],u - aa + (aa ^ a) + xx - x) #print(u,aa,aa^a,xx,x,nx) if u + a < nx[a]: nx[a] = u + a d = nx #print(d) ans = inf for k in d.values(): if k < ans:ans = k print(ans)