// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif



int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	int n;
	cin >> n;
	vector<int> pos(n), a(n);
	copy_n(istream_iterator<int>(cin), n, pos.begin());
	copy_n(istream_iterator<int>(cin), n, a.begin());
	vector<int> pref(n + 1);
	for(auto i = 0; i < n; ++ i){
		pref[i + 1] = pref[i] ^ a[i];
	}
	const long long inf = 3e18;
	vector<long long> dp(n + 1, inf);
	dp[0] = 0;
	for(auto l = 0; l < n; ++ l){
		for(auto r = l + 1; r <= n; ++ r){
			dp[r] = min(dp[r], dp[l] + (pref[r] ^ pref[l]) + pos[r - 1] - pos[l]);
		}
	}
	cout << dp[n] << "\n";
	return 0;
}

/*

*/