from collections import defaultdict, deque, Counter # 2つの数列 隣接swap 一致させる 要素に重複なし # 転倒数 def mergeCount(A): cnt = 0 n = len(A) if n > 1: A1 = A[:n >> 1] A2 = A[n >> 1:] cnt += mergeCount(A1) cnt += mergeCount(A2) i1 = 0 i2 = 0 for i in range(n): if i2 == len(A2): A[i] = A1[i1] i1 += 1 elif i1 == len(A1): A[i] = A2[i2] i2 += 1 elif A1[i1] <= A2[i2]: A[i] = A1[i1] i1 += 1 else: A[i] = A2[i2] i2 += 1 cnt += n//2 - i1 return cnt n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) if Counter(A) != Counter(B): print(-1) exit() # print(A) # print(B) # A[i] = B[s_i] となるような配列 S (s_0, s_1, ,,,)を考える # A[i], B[i]で要素の値が等しいものが複数あるときは、i < j, s_i < s_jとなるように決める # 決定したSに対して転倒数を求める # Bの要素の値ごとの位置を調べる pos = defaultdict(deque) for itr, b in enumerate(B): pos[b].append(itr) S = [] for a in A: p = pos[a].popleft() S.append(p) # print(S) print(mergeCount(S))