from collections import defaultdict, deque N, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) idx = defaultdict(list) for i, a in enumerate(A): idx[a].append(i) INF = 1 << 60 dp = [INF] * N dp[0] = 0 for b in B: que = deque() for i in range(N): if dp[i] < INF: que.append(i) l, r = 0, N - 1 ndp = [INF] * N for i in range(N): if que and que[0] <= i: l = que.popleft() if que: r = que[0] else: r = N - 1 if A[i] == b: ndp[i] = min(dp[l] + (i - l), dp[r] + (r - i)) else: ndp[i] = INF dp = ndp[:] print(min(dp))