# 2次元dp 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 k in pos[num]: for j in range(N): 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)