#include #include #include #include using namespace std; #define F first #define S second typedef long long int ll; constexpr int kN = int(1E5 + 10); int ABS(int n) {return n >= 0 ? n : -n;} struct BIT { int val[kN]; void init() {memset(val, 0, sizeof(val));} void add(int pos) { while (pos < kN) { val[pos]++; pos += pos & -pos; } return ; } int ask(int pos) { int ans = 0; while (pos) { ans += val[pos]; pos ^= pos & -pos; } return ans; } }; int a[kN], b[kN], difa[kN], difb[kN]; set> se; BIT bit; int main() { int n; ll ans = 0; set>::iterator u; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); if (a[1] != b[1] || a[n] != b[n]) { printf("-1\n"); return 0; } for (int i = 2; i <= n; i++) difa[i] = a[i] ^ a[i - 1]; for (int i = 2; i <= n; i++) difb[i] = b[i] ^ b[i - 1]; for (int i = 2; i <= n; i++) se.insert({difa[i], i}); bit.init(); for (int i = 2; i <= n; i++) { u = se.lower_bound({difb[i], 0}); if (u == se.end() || u -> F != difb[i]) { printf("-1\n"); return 0; } else { ans += ABS(u -> S - bit.ask(u -> S) - 2); bit.add(u -> S); se.erase(u); } } printf("%lld\n", ans); }