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: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <set>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second

typedef long long ll;

int a[100100], b[100100];
pair<int, int> A[100100], B[100100];
int p[100100];
int col[100100];
vector<int> com[100100];

int main() {
    int n;
    cin >> n;
    int x = 0;
    forn (i, n) {
        cin >> a[i];
        x ^= a[i];
        A[i] = mp(a[i], i);
    }
    a[n] = x;
    A[n] = mp(x, n);
    x = 0;
    forn (i, n) {
        cin >> b[i];
        x ^= b[i];
        B[i] = mp(b[i], i);
    }
    b[n] = x;
    B[n] = mp(x, n);
    ++n;
    sort(A, A + n);
    sort(B, B + n);
    bool ok = true;
    forn (i, n) {
        if (A[i].fs != B[i].fs) {
            ok = false;
        }
        p[A[i].sc] = B[i].sc;
    }
    if (!ok) {
        cout << -1 << endl;
        return 0;
    }
    int cur = 1;
    forn (i, n) {
        if (p[i] == i || col[i]) {
            continue;
        }
        col[i] = cur++;
        com[col[i]].pb(i);
        int v = p[i];
        while (v != i) {
            col[v] = col[i];
            com[col[v]].pb(v);
            v = p[v];
        }
    }
    forn (i, n - 1) {
        if (A[i].fs != A[i + 1].fs) {
            continue;
        }
        int x = A[i].sc, y = A[i + 1].sc;
        if (col[x] == col[y]) {
            continue;
        }
        if (col[x] == 0 && x < n - 1) {
            continue;
        }
        if (col[y] == 0 && y < n - 1) {
            continue;
        }
        if (com[col[x]].size() < com[col[y]].size()) {
            swap(x, y);
        }
//        cerr << x << " " << y << endl;
        int ccol = col[y];
        forn (j, com[ccol].size()) {
            int tmp = com[ccol][j];
            col[tmp] = col[x];
            com[col[x]].pb(tmp);
        }
    }
    long long ans = 0;
    forn (i, n) {
//        cerr << com[col[i]].size() << " ";
        if (col[i] == 0) {
            continue;
        }
        if (col[i] == col[n - 1]) {
            ans += com[col[i]].size() - 1;
        } else {
            ans += com[col[i]].size() + 1;
        }
        int ccol = col[i];
        forn (j, com[ccol].size()) {
            int tmp = com[ccol][j];
            col[tmp] = 0;
        }
    }
    cout << ans << endl;
    return 0;
}