from collections import defaultdict import sys input = sys.stdin.read def main(): data = input().split() N = int(data[0]) Q = int(data[1]) A = list(map(int, data[2:N+2])) B = list(map(int, data[N+2:])) value_to_indices = defaultdict(list) for index, value in enumerate(A, start=1): value_to_indices[value].append(index) current_position = 1 total_cost = 0 for target in B: possible_indices = value_to_indices[target] best_cost = float('inf') best_index = -1 for index in possible_indices: cost = abs(current_position - index) if cost < best_cost: best_cost = cost best_index = index total_cost += best_cost current_position = best_index print(total_cost) if __name__ == "__main__": main()