import sys MOD=10**9+7 class BIT(): def __init__(self,n): self.n=n self.a=[0]*(n+1) def add(self,k,v): while k<=self.n: self.a[k]+=v k+=k&-k def sum(self,k): ret=0 while k>0: ret+=self.a[k] k-=k&-k return ret N=int(input()) A=list(map(int,input().split()))+[0] B=list(map(int,input().split()))+[0] A=[A[i+1]^A[i] for i in range(0,N)] B=[B[i+1]^B[i] for i in range(0,N)] bit=BIT(N) m=dict() for b in B: m[b]=[] for i,b in enumerate(B): m[b].append(i) ans=0 for i in range(N-1,-1,-1): a=A[i] if a not in m or len(m[a])==0: exit(print(-1)) p=m[a][-1]+1 ans+=bit.sum(p) bit.add(p,1) m[a].pop() print(ans)