#include using namespace std; const long long INF = 1e18; int main() { int N, Q; cin >> N >> Q; vector A(N), B(Q); for (int i = 0; i < N; i++) { cin >> A[i]; } for (int i = 0; i < Q; i++) { cin >> B[i]; } map> m; for (int i = 0; i < N; i++) { m[A[i]].push_back(i); } vector> dp(Q + 1, vector (N, INF)); dp[0][0] = 0; for (int i = 0; i < Q; i++) { for (int j = 0; j < N; j++) { if (dp[i][j] != INF) { for (int w : m[B[i]]) { dp[i + 1][w] = min(dp[i + 1][w], dp[i][j] + abs(w - j)); } } } } long long ans = INF; for (int i = 0; i < N; i++) { ans = min(ans, dp[Q][i]); } cout << ans << endl; }