N,Q=map(int,input().split()) A=list(map(int,input().split())) B=list(map(int,input().split())) from collections import defaultdict D=defaultdict(list) for i,a in enumerate(A): D[a].append(i) dp=[[1<<60 for _ in range(N)] for _ in range(Q+1)] dp[0][0]=0 for i,b in enumerate(B,1): for j in range(N): if dp[i-1][j]<1<<60: for p in D[b]: dp[i][p]=min(dp[i][p],dp[i-1][j]+abs(p-j)) ans=1<<60 for j in range(N): ans=min(ans,dp[Q][j]) print(ans)