Problem:
Score : 1000 points

Problem Statement

There is a sequence of length N: a = (a_1, a_2, ..., a_N).
Here, each a_i is a non-negative integer.

Snuke can repeatedly perform the following operation:

Let the XOR of all the elements in a be x. Select an integer i (1 ≤ i ≤ N) and replace a_i with x.

Snuke's objective is to match a with another sequence b = (b_1, b_2, ..., b_N).
Here, each b_i is a non-negative integer.

Determine whether the objective is achievable, and find the minimum necessary number of operations if the answer is positive.

Constraints

2 ≤ N ≤ 10^5

a_i and b_i are integers.

0 ≤ a_i, b_i < 2^{30}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
b_1 b_2 ... b_N

Output

If the objective is achievable, print the minimum necessary number of operations.
Otherwise, print -1 instead.

Sample Input 1

3
0 1 2
3 1 0

Sample Output 1

2

At first, the XOR of all the elements of a is 3.
If we replace a_1 with 3, a becomes (3, 1, 2).

Now, the XOR of all the elements of a is 0.
If we replace a_3 with 0, a becomes (3, 1, 0), which matches b.

Sample Input 2

3
0 1 2
0 1 2

Sample Output 2

0

Sample Input 3

2
1 1
0 0

Sample Output 3

-1

Sample Input 4

4
0 1 2 3
1 0 3 2

Sample Output 4

5

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

template <class T>
void print(const T& a) {
    for (auto el : a) std::cout << el << " ";
    std::cout << std::endl;
}

std::deque<long long> cg[100100];

int main() {
    long long n;
    std::cin >> n;
    std::vector<long long> a(n + 1), b(n + 1);
    {
        std::map<long long, long long> cnt;
        long long x = 0;
        for (int i = 0; i < n; i++) {
            std::cin >> a[i];
            x ^= a[i];
            cnt[a[i]]++;
        }
        for (int i = 0; i < n; i++) {
            std::cin >> b[i];
            cnt[b[i]]--;
        }
        a[n] = x;
        cnt[x]++;
        for (auto& el : cnt) {
            if (el.second < 0) {
                std::cout << -1 << std::endl;
                return 0;
            }
            if (el.second > 0) x = el.first;
        }
        b[n] = x;
    }
    // print(a); print(b);
    {
        auto ca(a), cb(b);
        std::sort(ca.begin(), ca.end());
        ca.erase(std::unique(ca.begin(), ca.end()), ca.end());
        std::sort(cb.begin(), cb.end());
        cb.erase(std::unique(cb.begin(), cb.end()), cb.end());
        for (int i = 0; i <= n; i++) {
            a[i] = std::lower_bound(ca.begin(), ca.end(), a[i]) - ca.begin();
            b[i] = std::lower_bound(cb.begin(), cb.end(), b[i]) - cb.begin();
        }
    }
    
    // print(a); print(b);

    // std::vector<std::deque<long long>> cg(100010);
    std::set<long long> s;
    for (int i = 0; i <= n; i++) {
        if (a[i] != b[i]) {
            cg[a[i]].push_back(b[i]);
            s.insert(a[i]);
        }
    }

    auto ret = 0ll;
    while (!s.empty()) {
        // print(s);
        std::deque<long long> q;
        q.push_back(*s.begin());
        s.erase(s.begin());
        while (!q.empty()) {
            auto curr = *q.begin();
            if (curr == a[n]) ret--;
            ret += cg[curr].size();
            for (auto& to : cg[curr]) {
                if (s.find(to) != s.end()) {
                    q.push_back(to);
                    s.erase(s.find(to));
                }
            }
            q.pop_front();
        }
        ret++;
    }

    std::cout << ret - (a[n] == b[n] ? 0 : 1) << std::endl;

    return 0;
}