N = int(input()) X = list(map(int,input().split())) A = list(map(int,input().split())) from collections import defaultdict inf = 10 ** 20 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 in d: u = d[aa] nx[aa ^ a] = min(nx[aa ^ a],u - aa + (aa ^ a) + xx - x) #print(u,aa,aa^a,xx,x,nx) nx[a] = min(nx[a],u + a) d = nx #print(d) ans = inf for k in d.values(): if k < ans:ans = k print(ans)