/* -*- coding: utf-8 -*- * * 1209.cc: No.1209 XOR Into You - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; /* typedef */ typedef long long ll; typedef pair pii; template struct BIT { int n; T bits[MAX_N + 1]; BIT() {} BIT(int _n) { init(_n); } void init(int _n) { n = _n; memset(bits, 0, sizeof(bits)); } T sum(int x) { T s = 0; while (x > 0) { s += bits[x]; x -= (x & -x); } return s; } void add(int x, T v) { while (x <= n) { bits[x] += v; x += (x & -x); } } }; /* global variables */ int as[MAX_N], bs[MAX_N], qs[MAX_N]; pii pas[MAX_N], pbs[MAX_N]; BIT bit; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", as + i); for (int i = 0; i < n; i++) scanf("%d", bs + i); if (as[0] != bs[0] || as[n - 1] != bs[n - 1]) { puts("-1"); return 0; } int m = n - 1; for (int i = 0; i < m; i++) { pas[i] = pii(as[i] ^ as[i + 1], i); pbs[i] = pii(bs[i] ^ bs[i + 1], i); } sort(pas, pas + m); sort(pbs, pbs + m); for (int i = 0; i < m; i++) { if (pas[i].first != pbs[i].first) { puts("-1"); return 0; } qs[pas[i].second] = pbs[i].second; } //for (int i = 0; i < m; i++) printf("%d ", qs[i]); putchar('\n'); bit.init(m); ll sum = 0; for (int i = 0; i < m; i++) { sum += i - bit.sum(qs[i] + 1); bit.add(qs[i] + 1, 1); } printf("%lld\n", sum); return 0; }