#include #define int long long using namespace std; const int N = 5e5 + 10; const int inf = 1e18; int n,a[N],b[N],c[N]; map > mp; template struct Fenwick { int n; vector a,p; Fenwick(int n_ = 0) { init(n_); } void init(int n_) { n = n_; a.assign(n + 1, T{}); p.assign(n + 1, T{}); } void add(int x, const T &v) { p[x] = p[x] + v; for (int i = x; i <= n; i += i & -i) { a[i] = a[i] + v; } } T sum(int x) { T ans{}; for (int i = x; i > 0; i -= i & -i) { ans = ans + a[i]; } return ans; } T query(int l, int r) { T res{}; while(l <= r) { if(r - (r & (-r)) + 1 >= l) { res = res + a[r]; r -= r & (-r); } else { res = res + p[r]; r--; } } return res; } int select(const T &k) { int x = 0; T cur{}; for (int i = 1 << std::__lg(n); i; i /= 2) { if (x + i <= n && cur + a[x + i] <= k) { x += i; cur = cur + a[x]; } } return x; } }; void solve() { cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 1;i <= n;i++) cin >> b[i]; for(int i = 1;i <= n;i++) c[i] = a[i] ^ a[i - 1]; for(int i = n;i >= 1;i--) b[i] = b[i] ^ b[i - 1]; for(int i = n;i >= 1;i--) mp[b[i]].push_back(i); for(int i = 1;i <= n;i++) { if(mp[c[i]].empty()) return cout << -1 << '\n',void(); int tmp = c[i]; c[i] = mp[c[i]].back(); mp[tmp].pop_back(); // cout << c[i] << ' '; } // cout << '\n'; Fenwick f(n + 10); int ans = 0; for(int i = n;i >= 1;i--) { ans += f.sum(c[i]); f.add(c[i],1); } cout << ans << '\n'; } signed main() { // ios::sync_with_stdio(0); // cin.tie(0);cout.tie(0); int t = 1; while(t--) solve(); return 0; }