class fenwick_tree(): n=1 data=[0 for i in range(n)] def __init__(self,N): self.n=N self.data=[0 for i in range(N)] def add(self,p,x): assert 0<=p0): s+=self.data[r-1] r-=r&-r return s 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]: print(-1) exit() X = [] Y = [] for i in range(N-1): X.append(A[i+1] ^ A[i]) Y.append(B[i+1] ^ B[i]) if sorted(X) != sorted(Y): print(-1) exit() import queue Z = {} g = 0 for y in Y: if not y in Z: Z[y] = queue.deque([g]) g += 1 else: Z[y].append(g) g += 1 T = [] for x in X: T.append(Z[x].popleft()) X = T G = fenwick_tree(g) ans = 0 for x in X[::-1]: ans += G.sum0(x) G.add(x, 1) print(ans)