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 (Python):
class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans

from collections import Counter

N=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=0
for i in range(N):
    A^=a[i]
B=0
for i in range(N):
    B^=b[i]
a.append(A)
b.append(B)

if Counter(a)!=Counter(b):
    exit(print(-1))

val=set([])
for i in range(N+1):
    if a[i]!=b[i]:
        val.add(a[i])

if not val:
    exit(print(0))

val=list(val)
val.sort()
comp={i:e for e,i in enumerate(val)}
n=max(comp[d] for d in comp)+1

uf=UnionFindVerSize(n)
check=False
cnt=0
for i in range(N):
    if a[i]!=b[i]:
        uf.unite(comp[a[i]],comp[b[i]])
        if a[i]==b[-1]:
            check=True
        cnt+=1

print(cnt+uf.calc_group_num()-int(check))

Solution (Python):
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    ans = 0
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    class UnionFind():
        def __init__(self, n):
            self.parent = [-1 for _ in range(n)]
            

        def find(self, x):
            
            if self.parent[x] < 0:
                return x
            else:
                self.parent[x] = self.find(self.parent[x])
                return self.parent[x]

        def unite(self, x, y):
            
            x, y = self.find(x), self.find(y)
            if x == y:
                return False
            else:
                if self.size(x) < self.size(y):
                    x, y = y, x
                self.parent[x] += self.parent[y]
                self.parent[y] = x

        def same(self, x, y):
            
            return self.find(x) == self.find(y)

        def size(self, x):
            
            x = self.find(x)
            return -self.parent[x]

        def is_root(self, x):
            
            return self.parent[x] < 0

        def roots(self):
            
            return [i for i, x in enumerate(self.parent) if x < 0]

        def members(self, x):
            
            root = self.find(x)
            return [i for i in range(self.n) if self.find(i) == root]

        def group_count(self):
            
            return len(self.roots())

        def all_group_members(self):
            
            return {r: self.members(r) for r in self.roots()}

    N = I()
    A = LI()
    B = LI()
    uf = UnionFind(N+1)
    Da = defaultdict(list)
    Db = defaultdict(list)
    bita = 0

    same = [False]*(N+1)
    for i in range(N):
        if A[i]==B[i]:
            same[i] = True
    if sum(same)==N:
        print(0)
        return

    for i,a in enumerate(A):
        bita ^= a
        if same[i]:
            continue
        Da[a].append(i)
    Da[bita].append(N)
    bitb = 0
    for i,b in enumerate(B):
        bitb ^= b
        if same[i]:
            continue
        Db[b].append(i)
    Db[bitb].append(N)
    
    
    for key,a in Da.items():
        if len(Db[key])!=len(a):
            print(-1)
            return

    for key,a in Da.items():
        uf.unite(a[0],Db[key][0])
        if len(a)==1:
            continue
        for i in a[1:]:
            uf.unite(a[0],i)
    for b in Db.values():
        if len(b)==1:
            continue
        for i in b[1:]:
            uf.unite(b[0],i)

    if uf.size(N)==N-sum(same):
        ans = uf.size(N)
        if bita!=bitb:
            ans -= 1
        print(ans)
        return

    ans = 0
    used = [False]*(N+1)
    for i in range(N):
        
        if same[i]:
            continue
        p = uf.find(i)
        if used[p]:
            continue
        used[p] = True
        ans += (-uf.parent[p]+1)
        
    
    
    if uf.size(N)>1:
        ans -= 1
    if bita!=bitb or len(Da[bita])>1:
        ans -= 1
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

def test():
    i = I()
    li = LI()
    lsi = LSI()
    si = LS()
    print(i)
    print(li)
    print(lsi)
    print(si)
    return

from decimal import Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examD()

"""
5
0 2 3 6 9
2 9 0 6 3

5
0 2 3 6 9
2 9 0 6 14

5
0 2 3 6 9
2 3 0 6 14

5
0 2 3 6 9
2 0 3 9 14

4
1 2 4 8
2 1 8 4

"""

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from functools import reduce
import operator
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components

N = int(readline())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))

a_xor = reduce(operator.xor,A)
b_xor = reduce(operator.xor,B)

if any(x != y for x,y in zip(sorted(A + [a_xor]),sorted(B + [b_xor]))):
    print(-1)
    exit()

Adiff = [x for x,y in zip(A,B) if x != y]
Bdiff = [y for x,y in zip(A,B) if x != y]
x_to_i = {x:i for i,x in enumerate(set(Adiff + [a_xor]))}
row = [x_to_i[x] for x in Adiff]
col = [x_to_i[x] for x in Bdiff]
if a_xor != b_xor:
    row.append(x_to_i[a_xor])
    col.append(x_to_i[b_xor])
V = len(x_to_i)
graph = csr_matrix(([1]*len(row),(row,col)),(V,V))
Ncomp,comp = connected_components(graph)

counter = [0] * Ncomp
for x in Adiff:
    counter[comp[x_to_i[x]]] += 1
c = comp[x_to_i[a_xor]]
counter[c] += 1

answer = sum(x+1 for x in counter) - 2
print(answer)

Solution (Python):
from collections import defaultdict
from functools import reduce
from operator import xor


def dfs(s, links, fixed):
    q = [s]
    while q:
        v = q.pop()
        if fixed[v]:
            continue
        fixed[v] = True
        q.extend(links[v])


def solve(n, aaa, bbb):
    a0 = reduce(xor, aaa, 0)
    b0 = reduce(xor, bbb, 0)
    aaa.append(a0)
    bbb.append(b0)
    ad = defaultdict(set)
    bd = defaultdict(set)
    for i, a in enumerate(aaa):
        ad[a].add(i)
    for i, b in enumerate(bbb):
        bd[b].add(i)
    ac = {a: len(s) for a, s in ad.items()}
    bc = {b: len(s) for b, s in bd.items()}
    if ac != bc:
        return -1
    links = defaultdict(set)
    ans = 0
    for a, b in zip(aaa, bbb):
        if a == b:
            continue
        ans += 1
        links[a].add(b)

    if not ans:
        return 0

    if a0 == b0:
        links[a0].add(b0)
    else:
        ans -= 1

    fixed = {a: False for a in links}
    for a in links:
        if fixed[a]:
            continue
        ans += 1
        dfs(a, links, fixed)

    return ans - 1


n = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
print(solve(n, aaa, bbb))

Solution (Python):
n,a,b=int(input()),list(map(int,input().split()))+[0],list(map(int,input().split()))+[0]
for i in range(n):
	a[n]^=a[i]
	b[n]^=b[i]
na,nb=sorted(a),sorted(b)
if na!=nb:
	print("-1")
	exit()
f=dict()
def find(x):
	if f[x]==x:
		return x
	else:
		f[x]=find(f[x])
		return f[x]
ans=0
for i in range(n):
	if a[i]!=b[i]:
		f[a[i]]=a[i]
f[a[n]]=a[n]
for i in range(n):
	if a[i]!=b[i]:
		ans+=1
		f[find(b[i])]=find(a[i])
for i in f:
	if i==f[i]:
		ans+=1
print(ans-1)