import sys, random input = lambda : sys.stdin.readline().rstrip() write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x)) debug = lambda x: sys.stderr.write(x+"\n") YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO); INF=10**18 LI = lambda v=0: list(map(lambda i: int(i)-v, input().split())); II=lambda : int(input()); SI=lambda : [ord(c)-ord("a") for c in input()] def debug(_l_): for s in _l_.split(): print(f"{s}={eval(s)}", end=" ") print() def dlist(*l, fill=0): if len(l)==1: return [fill]*l[0] ll = l[1:] return [dlist(*ll, fill=fill) for _ in range(l[0])] n = int(input()) x = LI() a = LI() cuma = [0] for v in a: cuma.append(cuma[-1]^v) cumx = [0] for v in x: cumx.append(cumx[-1]+v) dp = [INF]*(n+1) dp[0] = 0 def dist(j,i): return abs(x[j]-x[i-1]) mid = (i-1+j)//2 # d = sum((abs(x[mid]-x[ind]) for ind in range(j,i))) # d = (x[mid]*(mid-j)) - (cumx[mid]-cumx[j]) + ((cumx[i]-cumx[mid]) - (x[mid]*(i-mid))) return d for i in range(1,n+1): val = INF for j in range(i): d = dist(j,i) tmp = dp[j] + (cuma[i]^cuma[j]) + d val = min(val, tmp) dp[i] = val print(dp[n])