N, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) data = [] current_index = 0 for idx, b in enumerate(B): if idx == 0: target_indexes = [i for i, j in enumerate(A) if j == b] res = [] for index in target_indexes: cost = abs(index - current_index) res.append({"index": index, "cost": cost}) data.append(res) else: current_indexes = data[idx - 1] target_indexes = [i for i, j in enumerate(A) if j == b] res = [] for c_index in current_indexes: for t_index in target_indexes: cost = c_index["cost"] + abs(t_index - c_index["index"]) res.append({"index": t_index, "cost": cost}) data.append(res) print(sorted(data[N - 2], key=lambda x: x["cost"])[0]["cost"])