N, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) INF = 10 ** 6 dp = [0] + [INF] * (N - 1) for b in B: dp1 = [INF] * N dp2 = [INF] * N for i in range(N): j = N - i - 1 if i == 0: dp1[i] = dp[i] dp2[j] = dp[j] else: dp1[i] = min(dp[i], dp1[i - 1] + 1) dp2[j] = min(dp[j], dp2[j + 1] + 1) dp = [INF] * N for i in range(N): if A[i] == b: dp[i] = min(dp1[i], dp2[i]) print(min(dp))