n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) # Bの各要素の位置を記録 pos_b = [0] * (n + 1) # 値は1〜nなので、0番目は使わない for idx in range(n): val = B[idx] pos_b[val] = idx # AをBの位置に変換して配列Cを作成 C = [] for val in A: C.append(pos_b[val]) # 転倒数を計算するためのFenwick Tree class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 2) # 1-based indexing def update(self, idx, delta): # idxは1-based while idx <= self.n: self.tree[idx] += delta idx += idx & -idx def query(self, idx): # 1-basedのidxまでの合計を返す res = 0 while idx > 0: res += self.tree[idx] idx -= idx & -idx return res ft = FenwickTree(n) ans = 0 # 逆順に処理して転倒数を計算 for x in reversed(C): ans += ft.query(x) ft.update(x + 1, 1) # xは0-basedなので、1-basedのx+1に更新 print(ans)