N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
s=0
for i in A:
  s=s^i
if sorted(A)==sorted(B):
  ans=0
  for i in range(N):
    if A[i]!=B[i]:
      ans+=1
  if s not in B:
    if A==B:
      print(0)
    else:
      print(ans+1)
    exit()
  m=s
  L=list()
  for i in range(N):
    L.append(m)
    m=A[B.index(m)]
  if len(set(L))==ans:
    print(ans)
  else:
    print(ans+1)
  exit()
else:#OK
  ans=0
  A+=[s]
  L=sorted(A)
  R=sorted(B)
  for i in range(N):
    if L[i]!=R[i]:
      L.remove(L[i])
      break
    if i==N-1:
      L.remove(L[N])
  if sorted(L)!=sorted(R):
    print(-1)
    exit()
  for i in range(N):
    if A[i]!=B[i]:
      ans+=1
  print(ans)