B = int(input())
N = int(input())
C = sorted([int(input()) for _ in range(N)])
target = min(C[N // 2], (sum(C) + B) // N)
print(sum(abs(c - target) for c in C))