import sys sys.setrecursionlimit(5*10**5) input = sys.stdin.readline from collections import defaultdict, deque, Counter from heapq import heappop, heappush from bisect import bisect_left, bisect_right from math import gcd n = int(input()) x = list(map(int,input().split())) a = list(map(int,input().split())) sum = [[0]*n for i in range(n)] for l in range(n): now = 0 for r in range(l, n): now ^= a[r] sum[l][r] = now + x[r] - x[l] dp = [10**15]*(n+1) dp[0] = 0 for i in range(1, n+1): for j in range(1,i+1): tmp = sum[j-1][i-1] dp[i] = min(dp[i],tmp+dp[j-1]) print(dp[-1])