#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x ostream& operator<<(ostream& os, const pair& p){ return os << "{" << p.first << ", " << p.second << "}"; } template ostream& operator<<(ostream& os, const vector& obj) { os << "{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const set& obj) { os << "set{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const map& obj) { os << "map{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } #ifdef ONLINE_JUDGE #define dump(expr) ; #else #define dump(expr) { cerr << "\033[33m#L" << __LINE__ << ": " << expr << "\033[39m" << endl; } #endif template struct BinaryIndexedTree { int size; vector ft; BinaryIndexedTree(int size_) : size(size_), ft(size, (T)0) {} void add(int i, T value) { // val[i] += value for (; i < size; i |= i + 1) ft[i] += value; } T sum(int i) { // val[0] + ... + val[i] T s = 0; for (; i >= 0; i = (i & (i + 1)) - 1) s += ft[i]; return s; } }; ll compute_inversion_number(vector vs) { // vs must be a permutation of (0, 1, ..., n - 1) int n = static_cast(vs.size()); BinaryIndexedTree bt(n); ll res = 0LL; for (int i = n - 1; i >= 0; --i) { res += bt.sum(vs[i]); bt.add(vs[i], 1); } return res; } ll solve() { int n; cin >> n; vector vs(n), vt(n); for (int i : range(n)) cin >> vs[i]; for (int i : range(n)) cin >> vt[i]; if (vs[0] != vt[0]) return -1; if (vs[n - 1] != vt[n - 1]) return -1; for (int i : range(n - 1)) vs[i] ^= vs[i + 1], vt[i] ^= vt[i + 1]; vs.resize(n - 1); vt.resize(n - 1); n--; map, int> mp; map count; for (int i : range(n)) { int t = vt[i]; int c = count[t]; mp[{t, c}] = i; count[t]++; } count.clear(); for (int i : range(n)) { int s = vs[i]; int c = count[s]; if (!mp.count({s, c})) return -1; vs[i] = mp[{s, c}]; count[s]++; } dump(vs); return compute_inversion_number(vs); } int main() { cout << fixed << setprecision(12); cout << solve() << endl; }