N = int(input()) X = list(map(int, input().split())) A = list(map(int, input().split())) B = [0] for i in range(N): B.append(B[-1] ^ A[i]) dp0 = [[0] * (N + 1) for _ in range(N + 1)] for d in range(1, N + 1): for l in range(N): r = l + d if r > N: continue dp0[l][r] = (B[r] ^ B[l]) + X[r - 1] - X[l] dp = [0] * (N + 1) for r in range(1, N + 1): dp[r] = dp0[0][r] for l in range(r): dp[r] = min(dp[r], dp[l] + dp0[l][r]) print(dp[-1])