import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static long[][] dp; static int[] positions; static int[] values; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); positions = new int[n]; for (int i = 0; i < n; i++) { positions[i] = sc.nextInt(); } values = new int[n]; for (int i = 0; i < n; i++) { values[i] = sc.nextInt(); } dp = new long[n][n]; for (long[] arr : dp) { Arrays.fill(arr, -1); } System.out.println(dfw(0, n - 1)); } static long dfw(int left, int right) { if (dp[left][right] < 0) { dp[left][right] = Long.MAX_VALUE; int current = 0; long distance = 0; for (int i = left; i < right; i++) { dp[left][right] = Math.min(dp[left][right], dfw(left, i) + dfw(i + 1, right)); current ^= values[i]; distance += Math.abs(positions[i] - positions[(left + right) / 2]); } current ^= values[right]; distance += Math.abs(positions[right] - positions[(left + right) / 2]); dp[left][right] = Math.min(dp[left][right], current + distance); } return dp[left][right]; } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }