INF = 1 << 60 B = int(input()) N = int(input()) C = [int(input()) for _ in range(N)] C.sort(reverse=True) def f(v: int) -> int: t = B res = 0 for c in C: if c > v: # 多い t += c - v res += c - v elif c < v: # 少ない if v - c > t: return INF t -= v - c res += v - c return res res = f(C[N // 2]) #if N % 2 == 0: # res = min(res, f(C[N // 2 - 1])) print(res)