inf = 10 ** 15 N, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) dp_left = [0] * N dp_right = [inf] * N dp_right[0] = 0 for i in B: ndp = [inf] * N for j in range(N): if A[j] == i: ndp[j] = min(dp_left[j] + j, dp_right[j] - j) ndp_left = [inf] * N ndp_left[0] = ndp[0] ndp_right = [inf] * N for j in range(1, N): ndp_left[j] = min(ndp_left[j - 1], ndp[j] - j) ndp_right[N - 1] = ndp[N - 1] + (N - 1) for j in range(N - 1, 0, -1): ndp_right[j - 1] = min(ndp_right[j], ndp[j - 1] + (j - 1)) dp_left = ndp_left dp_right = ndp_right print(min(min(dp_left[i] + i for i in range(N)), min(dp_right[i] - i for i in range(N))))