import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np data = np.fromstring(read(), np.int64, sep=' ') B = data[0] N = data[1] C = data[2:] INF = 10 ** 18 def f(x): if C.sum() + B < x * N: return INF return np.abs(C-x).sum() cand = C.tolist() + [(B + C.sum())//N] answer = min(f(x) for x in cand) print(answer)