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[N-1] != B[N-1]: print(-1) exit() A = [A[i+1]^A[i] for i in range(N-1)] B = [B[i+1]^B[i] for i in range(N-1)] if sorted(A) != sorted(B): print(-1) exit() X = {} for i, b in enumerate(B): if b in X: X[b].append(i) else: X[b] = [i] cur = {x:0 for x in X} Z = [] for a in A: Z.append(X[a][cur[a]]) cur[a] += 1 ft = fenwick_tree(N-1) ans = 0 for z in Z[::-1]: ans += ft.sum0(z) ft.add(z, 1) print(ans)