import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) sys.setrecursionlimit(10**7) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### # Binary Indexed Tree (Fenwick Tree) # 0-indexed class BIT: def __init__(self, n): self.n = n self.data = [0]*(n+1) self.el = [0]*(n+1) def sum(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s def add(self, i, x): assert 0 <= i < self.n i = i+1 self.el[i] += x while i <= self.n: self.data[i] += x i += i & -i def get(self, i, j=None):# j=Noneのときiを取得, [i, j)の和を取得 if j is None: return self.el[i+1] return self.sum(j) - self.sum(i) #順列の転倒数を求める def tentou(ar): n = len(ar) bit = BIT(n) ans = 0 for i, p in enumerate(ar): bit.add(p, 1) ans += i-bit.sum(p) return ans n = ni() a = na() b = na() if a[0]!=b[0] or a[-1]!=b[-1]: print(-1) exit() za = [a[i+1]^a[i]for i in range(n-1)] zb = [b[i+1]^b[i]for i in range(n-1)] from collections import Counter if Counter(za)!=Counter(zb): print(-1) exit() from collections import defaultdict d = defaultdict(list) for i in range(n-1): d[zb[i]].append(i) c = [-1 for i in range(n-1)] for i in range(n-2,-1,-1): c[i] = d[za[i]].pop() print(tentou(c))