#include <bits/stdc++.h> using namespace std; typedef long long ll; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; } #define all(x) (x).begin(),(x).end() #define fi first #define se second #define mp make_pair #define si(x) int(x.size()) const int mod=998244353,MAX=1005,INF=15<<26; #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") int dp[MAX][MAX]; int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); int N,Q;cin>>N>>Q; vector<int> A(N),B(Q); for(int i=0;i<N;i++) cin>>A[i]; for(int i=0;i<Q;i++) cin>>B[i]; for(int i=0;i<MAX;i++) for(int j=0;j<MAX;j++) dp[i][j]=INF; dp[0][0]=0; for(int i=0;i<Q;i++){ for(int j=0;j<N;j++){ if(dp[i][j]==INF) continue; for(int k=0;k<N;k++){ if(A[k]==B[i]) chmin(dp[i+1][k],dp[i][j]+abs(j-k)); } } } int ans=INF; for(int j=0;j<N;j++) chmin(ans,dp[Q][j]); cout<<ans<<endl; }