#include #include #include #include using namespace std; class BIT { public: int size_ = 1; vector bit; void init(int sz) { size_ = sz + 2; bit.resize(size_ + 2, 0); } void add(int pos, long long x) { pos++; while (pos <= size_) { bit[pos] += x; pos += (pos & -pos); } } long long sum(int pos) { long long s = 0; pos++; while (pos >= 1) { s += bit[pos]; pos -= (pos & -pos); } return s; } }; long long getInversions(vector dat) { BIT Z; Z.init(dat.size() + 2); long long sum = 0; for (int i = 0; i < dat.size(); i++) { sum += Z.sum(dat.size() + 1) - Z.sum(dat[i]); Z.add(dat[i], 1); } return sum; } // 変数 long long N, A[1 << 18], B[1 << 18]; long long D1[1 << 18], E1[1 << 18]; long long D2[1 << 18], E2[1 << 18]; int cnt[1 << 18]; vector V[1 << 18]; int main() { // Step #1. 入力 cin >> N; for (int i = 1; i <= N; i++) cin >> A[i]; for (int i = 1; i <= N; i++) cin >> B[i]; assert(N >= 3 && N <= 100000); // Step #2. 階差を取る vector vec1, vec2; for (int i = 1; i <= N - 1; i++) D1[i] = (A[i + 1] ^ A[i]); for (int i = 1; i <= N - 1; i++) D2[i] = (B[i + 1] ^ B[i]); for (int i = 1; i <= N - 1; i++) vec1.push_back(D1[i]); for (int i = 1; i <= N - 1; i++) vec2.push_back(D2[i]); sort(vec1.begin(), vec1.end()); sort(vec2.begin(), vec2.end()); if (vec1 != vec2) { cout << "-1" << endl; return 0; } // Step #3. 転倒数を求める for (int i = 1; i <= N - 1; i++) { E1[i] = lower_bound(vec1.begin(), vec1.end(), D1[i]) - vec1.begin(); E2[i] = lower_bound(vec2.begin(), vec2.end(), D2[i]) - vec2.begin(); V[E1[i]].push_back(i); } vector res; for (int i = 1; i <= N - 1; i++) { res.push_back(V[E2[i]][cnt[E2[i]]]); cnt[E2[i]] += 1; } // Step #4. 答えを求める long long Answer = getInversions(res); cout << Answer << endl; return 0; }