from collections import defaultdict class fenwick_tree(object): def __init__(self, n): self.n = n self.log = n.bit_length() self.data = [0] * n def __sum(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s def add(self, p, x): """ a[p] += xを行う""" p += 1 while p <= self.n: self.data[p - 1] += x p += p & -p def sum(self, l, r): """a[l] + a[l+1] + .. + a[r-1]を返す""" return self.__sum(r) - self.__sum(l) def lower_bound(self, x): """a[0] + a[1] + .. a[i] >= x となる最小のiを返す""" if x <= 0: return -1 i = 0 k = 1 << self.log while k: if i + k <= self.n and self.data[i + k - 1] < x: x -= self.data[i + k - 1] i += k k >>= 1 return i def main(): N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) if A[0] != B[0] or A[-1] != B[-1]: return -1 A = [a ^ b for a, b in zip(A[:-1], A[1:])] B = [a ^ b for a, b in zip(B[:-1], B[1:])] btoi = defaultdict(list) for i, b in enumerate(B): btoi[b].append(i) ans = 0 bit = fenwick_tree(len(A)) for a in reversed(A): if not btoi[a]: return - 1 i = btoi[a].pop() ans += bit.sum(0, i) bit.add(i, 1) return ans print(main())