N = int(input()) X = list(map(int, input().split())) A = list(map(int, input().split())) cumXOR = [0] for i in range(N): cumXOR.append(cumXOR[-1]^A[i]) def rangeXOR(l, r): return cumXOR[r+1]^cumXOR[l] INF = 10**18 dp = [INF]*(N+1) dp[0] = 0 for i in range(N): for j in range(i+1): left = dp[j] if j < i: cost = X[i]-X[j] else: cost = 0 xor = rangeXOR(j, i) dp[i+1] = min(dp[i+1], left+cost+xor) print(dp[-1])