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 <bits/stdc++.h>
using namespace std;

#define times(n, i)      uptil(0, n, i)
#define rtimes(n, i)     downto((n) - 1, 0, i)
#define upto(f, t, i)    for(int _##i = (t), i = (f); i <= _##i; i++)
#define uptil(f, t, i)   for(int _##i = (t), i = (f); i <  _##i; i++)
#define downto(f, t, i)  for(int _##i = (t), i = (f); i >= _##i; i--)
#define downtil(f, t, i) for(int _##i = (t), i = (f); i >  _##i; i--)
typedef long double ld;
#define long long long

#if defined(EBUG) && !defined(ONLINE_JUDGE)
    #define debug true
    #define ln << endl
#else
    #define debug false
    #define ln << '\n'
#endif
#define tb << '\t'
#define sp << ' '

const int MOD = 1e9+7;

int N, A[100008], B[100008], c[100008], cn, d[100008], db[100008], ci[100008], v[100008], vc[100008], pi[100008];
map<int, int> cc;
vector<int> path[100008];

signed main() { // long: 64bit
    if(!debug) {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }

    int N;
    scanf("%d",&N);
    times(N, i) {
        scanf("%d",&A[i]);
        A[N] ^= A[i];
    }
    times(N, i) {
        scanf("%d",&B[i]);
        B[N] ^= B[i];
    }

    times(N+1, i) c[i] = A[i];
    sort(c, c+N+1);
    cn = unique(c, c+N+1) - c;
    times(cn, i) cc[c[i]] = i;

    times(N+1, i) {
        ++d[cc[A[i]]]; ++db[cc[B[i]]];
        path[cc[A[i]]].push_back(cc[B[i]]);
        //cout << A[i] tb << B[i] ln;
    }
    if(debug) {
        times(cn, i) {
            cout << i tb << c[i] tb << cc[c[i]] tb << d[i] tb << db[i] << "|";
            for(int j: path[i]) cout tb << j;
            cout ln;
        }
        cout << string(100, '-') ln;
    }
    times(cn, i) {
        if(d[i] != db[i]) {
            cout << "-1\n"; return 0;
        }
    }

    int x = cc[A[N]], ans = 0;
    if(path[x][0] == x) {
        vc[0] = true;
    } else {
        do {
            ++ans;
            vc[x] = true;
            int y;
            do {
                y = path[x][pi[x]];
                ++pi[x];
            } while(y != cc[A[N]] && x == y);
            x = y;
            if(debug) cout << y << ">\t";
        } while(x != cc[A[N]]);
    }
    if(debug) cout << ans ln;

    times(cn, i) while(pi[i] < path[i].size()) {
        if(path[i][pi[i]] == i) { pi[i]++; continue; }
        bool independent = true;
        int x = i;
        do {
            ++ans;
            if(vc[x]) independent = false;
            int y;
            do {
                y = path[x][pi[x]];
                ++pi[x];
            } while(y != i && x == y);
            x = y;
            if(debug) cout << y << ">\t";
        } while(x != i);

        if(independent) ++ans;
        if(debug) cout << i << "\t" << independent << "\t" << ans ln;
    }
    cout << ans ln;

    return 0;
}