B = int(input()) N = int(input()) C = [int(input()) for i in range(N)] import statistics x = min(statistics.median_low(C), (sum(C) + B) // N) print(sum([abs(c - x) for c in C]))