import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = sc.nextInt(); } int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } sc.close(); int[] b = new int[n + 1]; for (int i = 0; i < n; i++) { b[i + 1] = b[i] ^ a[i]; } long[][] dp = new long[n][n]; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { dp[i][j] += x[j] - x[i]; dp[i][j] += b[j + 1] ^ b[i]; } } long inf = 1000000000000000000L; long[] dp2 = new long[n + 1]; Arrays.fill(dp2, inf); dp2[0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) { long val = dp2[j] + dp[j][i - 1]; dp2[i] = Math.min(dp2[i], val); } } System.out.println(dp2[n]); } }