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 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    vector<int> b(n);
    rep(i,n) cin >> b[i];
    int sa = 0;
    int sb = 0;
    rep(i,n){
        sa^=a[i];
        sb^=b[i];
    }
    a.push_back(sa);
    b.push_back(sb);
    n++;
    map<int,int>mpa,mpb;

    rep(i,n){
        mpa[a[i]]++;
        mpb[b[i]]++;
    }
    for(auto x:mpa){
        if(x.second!=mpb[x.first]){
            cout << -1 << endl;
            return 0;
        }
    }
    int c = 0;
    map<int,multiset<int> >mp;
    rep(i,n-1){
        if(a[i]!=b[i]){
            c++;
            mp[b[i]].insert(a[i]);
        }
    }
    if(c==0){
        cout << 0 << endl;
    }else{
        int p = sa;
        int res = 0;
        // cerr << c << endl;
        // while(!mp.empty()){
        //     //cerr << p << endl;
        //     if(mp.find(p)!=mp.end()){
        //         //cerr << "Test" << endl;
        //         auto& st = mp[p];
        //         auto x = st.begin();
        //         res++;
        //         int pre = p;
        //         p = *x;
        //         st.erase(x);
        //         //cerr << p << " " << st.size() << endl;
        //         if(st.size()==0){
        //             mp.erase(pre);
        //         }
        //         //cerr << "Test" << endl;
        //     }else{
        //         //cerr << "test" << endl;
        //         auto st = mp.begin();
        //         auto x = (*st).second.begin();
        //         //cerr <<(*st).first << " " << (*st).second.size() << " " << *x << endl;
        //         res++;
        //         mp[(*st).first].insert(p);
        //         p = *x;
        //         (*st).second.erase(x);
        //         if((*st).second.size()==0){
        //             mp.erase(st);
        //         }
        //         //cerr << "test" << endl;
        //     }
        //     //cerr << mp.size() << endl;
        // }
        if(sa==sb){
            cout << c+1 << endl;
        }else{
            cout <<c << endl;
        }
    }
    return 0;
}