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");
    }
}