B = int(input()) N = int(input()) C = [int(input()) for _ in range(N)] INF = 10 ** 10 + 1 maxx = (B + sum(C)) // N C.sort() D = [min(maxx, C[N // 2])] if N % 2 == 0: D.append(min(maxx, C[N // 2 - 1])) ans = INF for j in D: tmp = 0 for i in range(N): tmp += abs(C[i] - j) ans = min(ans, tmp) print(ans)