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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = int.Parse(Console.ReadLine());
        string[] str = Console.ReadLine().Split(' ');
        long[] A = new long[N];
        for(int i=0;i<N;i++){
            A[i] = int.Parse(str[i]);
        }
        long[] B = new long[N];
        str = Console.ReadLine().Split(' ');
        for(int i=0;i<N;i++){
            B[i] = int.Parse(str[i]);
        }
        long xor = 0;
        {
            bool b = true;
            for(int i=0;i<N;i++){
                xor ^= A[i];
                b &= A[i] == B[i];
            }
            if(b){
                sb.Append("0\n");
                return;
            }
            Array.Sort(B,A);
            long[] C = new long[N+1];
            for(int i=0;i<N;i++){
                C[i] = A[i];
            }
            C[N] = xor; 
            Array.Sort(C);
            int state = 0;
            int deleted = -1;
            for(int i=0;i<=N;i++){
                if(state == 0){
                    if(i == N){
                        deleted = N-1;
                        break;
                    }
                    if(B[i] != C[i]){
                        deleted = i;
                        state = 1;
                    }
                }
                else if(state == 1){
                    if(B[i-1] != C[i]){
                        sb.Append("-1\n");
                        return;
                    }
                }
            }
            B[deleted] = xor;
            Array.Sort(B,A);
        }
        int[] D = new int[N];
        for(int i=0;i<N;i++){
            D[i] = i;
        }
        Array.Sort(D,(x,y)=>A[x] - A[y] > 0 ? 1 : (A[x] == A[y] ? 0 : -1));
        int count = 0;
        bool[] us = new bool[N];
        for(int i=0;i<N;i++){
            if(!us[i]){
                int start = i;
                int p = i;
                us[i] = true;
                while(D[p] != start){
                    count++;
                    p = D[p];
                    us[p] = true;
                }
            }
        }
        count *= 3;
        count--;
        sb.Append(count+"\n");
    }
}