from collections import Counter
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

bonusNumber = 0
for i in range(n):
    bonusNumber ^= a[i]

aa = Counter({bonusNumber: 1})
bb = Counter()

for i in range(n):
    aa[a[i]] += 1
    bb[b[i]] += 1

for x in bb.keys():
    if bb[x] > aa[x]:
        print(-1)
        exit()

#############################
ans = 0
tmp = []
first = True
for i in range(n):
    if first and b[i] == bonusNumber and b[i] != a[i]:
        bonusNumber = a[i]
        a[i] = b[i]
        ans += 1
        first = False
    elif b[i] != a[i]:
        tmp.append(b[i])

while bonusNumber in tmp:
    tmp.remove(bonusNumber)
    bonusNumber = a[b.index(bonusNumber)]
    ans += 1

if len(tmp) == 0:
    print(ans)
else:
    ans += len(tmp) + 1
    print(ans)
