#include using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; 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]; } vector> dp(Q + 1, vector(N + 1, INFL)); dp[0][0] = 0; for (int i = 0; i < Q; i++) { vector pref(N + 1, INFL), suf(N + 1, INFL); for (int j = 0; j < N; j++) { pref[j + 1] = min(pref[j], dp[i][j] - j); } for (int j = N - 1; j >= 0; j--) { suf[j] = min(suf[j + 1], dp[i][j] + j); } for (int j = 0; j < N; j++) { if (A[j] == B[i]) { dp[i + 1][j] = min({dp[i + 1][j], pref[j + 1] + j, suf[j] - j}); } } } ll ans = INFL; for (int i = 0; i <= N; i++) { ans = min(ans, dp[Q][i]); } cout << ans << endl; }