N = int(input()) X = list(map(int, input().split())) A = list(map(int, input().split())) Ac = [0] * (N + 1) for i in range(N): Ac[i+1] = Ac[i] ^ A[i] inf = 10 ** 18 dp = [inf] * (N + 1) dp[0] = 0 for i in range(1, N+1): for j in range(i): dp[i] = min(dp[i], dp[j] + (Ac[i] ^ Ac[j]) + X[i-1] - X[j]) print(dp[-1])