from collections import Counter, defaultdict


class UnionFind:
    def __init__(self, size):
        self.table = [-1 for _ in range(size)]

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

    def union(self, x, y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            self.table[s2] = s1
            return True
        return False


def solve(a, b):
    axor, bxor = 0, 0
    for k in a:
        axor ^= k
    for k in b:
        bxor ^= k
    a += [axor]
    b += [bxor]
    if Counter(a) != Counter(b):
        return -1
    uft = UnionFind(n + 1)
    idx = defaultdict(set)
    same = 0

    for i, k in enumerate(a):
        idx[k].add(i)
    for i, k in enumerate(b):
        for j in idx[k]:
            if i == j:
                uft.table[i] = -2
                continue
            uft.union(i, j)
    return n - uft.table[:-1].count(-2) + max(0, uft.table[:-1].count(-1) - 1)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(solve(a, b))
