N = int(input()) X = list(map(int, input().split())) A = list(map(int, input().split())) F = [0] * (N + 1) X = [0] + X A = [0] + A for i in range(N + 1): F[i] = F[i - 1] ^ A[i] INF = 16 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] + (F[j] ^ F[i]) + (X[i] - X[j+1])) print(dp[N])