class BIT: def __init__(self, n): self.n = n self.data = [0] * (n + 1) if n == 0: self.n0 = 0 else: self.n0 = 1 << (n.bit_length() - 1) def sum_(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s def sum(self, l, r=-1): if r == -1: return self.sum_(l) else: return self.sum_(r) - self.sum_(l) def get(self, i): return self.sum(i, i + 1) def add(self, i, x): i += 1 while i <= self.n: self.data[i] += x i += i & -i def lower_bound(self, x): if x <= 0: return 0 i = 0 k = self.n0 while k > 0: if i + k <= self.n and self.data[i + k] < x: x -= self.data[i + k] i += k k //= 2 return i + 1 n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) if A[0] != B[0]: print(-1) exit() def f(A): return [A[i] ^ A[i + 1] for i in range(len(A) - 1)] A = f(A) B = f(B) if sorted(A) != sorted(B): print(-1) exit() dic = {} for i, a in enumerate(A): dic.setdefault(a, []).append(i) bit = BIT(n) ans = 0 for b in B[::-1]: i = dic[b].pop() ans += bit.sum(i) bit.add(i, 1) print(ans)