# 2次元dp # TLEしたのでstdin、それでもTLEしたのでdpの更新の順番で枝刈り import sys input = sys.stdin.readline from collections import defaultdict N, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) pos = defaultdict(list) for i in range(N): pos[A[i]].append(i) #print(pos) INF = 10**10 dp = [[INF]*N for i in range(Q+1)] dp[0][0] = 0 #print(dp[0]) for i in range(1, Q+1): num = B[i-1] for j in range(N): if dp[i-1][j] == INF: continue for k in pos[num]: dp[i][k] = min(dp[i][k], dp[i-1][j]+abs(j-k)) #print('num', num, 'pos[num]', pos[num], dp[i]) ans = INF for k in pos[B[-1]]: ans = min(ans, dp[Q][k]) print(ans)