n, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) inf = 1 << 60 dp = [inf] * n dp[0] = 0 for b in B: ndp = [inf] * n mi = inf for i in range(n): a = A[i] mi = min(mi + 1, dp[i]) if a == b: ndp[i] = min(ndp[i], mi) mi = inf for i in range(n - 1, -1, -1): a = A[i] mi = min(mi + 1, dp[i]) if a == b: ndp[i] = min(ndp[i], mi) dp = ndp print(min(dp))