import bisect from collections import defaultdict N,Q = list(map(int,input().split())) A = list(map(int,input().split())) B = list(map(int,input().split())) place = defaultdict(list) for i in range(N): place[A[i]].append(i) INF = 10**18 now = defaultdict(lambda:INF) now[0] = 0 for i in range(Q): next = defaultdict(lambda:INF) for j in now: left = bisect.bisect_right(place[B[i]],j) - 1 if(left != -1): l = place[B[i]][left] next[l] = min(next[l], now[j] + abs(j - l)) right = bisect.bisect_left(place[B[i]],j) if(right != len(place[B[i]])): r = place[B[i]][right] next[r] = min(next[r], now[j] + abs(j - r)) now,next = next,now print(min(now.values()))