#include using namespace std; void fast_io() { ios::sync_with_stdio(false); std::cin.tie(nullptr); } int main() { fast_io(); int n; cin >> n; vector x(n), a(n), a_cum(n + 1); for (int i = 0; i < n; i++) { cin >> x[i]; } for (int i = 0; i < n; i++) { cin >> a[i]; a_cum[i + 1] = a_cum[i] ^ a[i]; } const long long INF = 1e18; vector dp(n + 1, INF); dp[0] = 0; for (int r = 1; r <= n; r++) { for (int l = 0; l < r; l++) { dp[r] = min(dp[r], dp[l] + (a_cum[r] ^ a_cum[l]) + x[r - 1] - x[l]); } } cout << dp[n] << endl; }