# 谷、つまり1極値を求める三分探索での解き方 # https://roiti46.hatenablog.com/entry/2015/04/29/yukicoder_No.198_%E3%82%AD%E3%83%A3%E3%83%B3%E3%83%87%E3%82%A3%E3%83%BC%E3%83%BB%E3%83%9C%E3%83%83%E3%82%AF%E3%82%B9%EF%BC%92 # https://qiita.com/ganyariya/items/1553ff2bf8d6d7789127 B = int(input()) N = int(input()) C = [int(input()) for i in range(N)] ans = 10**18 l, r = -1, 10**18 while r-l > 2: lm, rm = (2*l+r)//3, (l+2*r)//3 if sum([lm-c for c in C]) > B: r = lm elif sum([rm-c for c in C]) > B: r = rm elif sum([abs(lm-c) for c in C]) <= sum([abs(rm-c) for c in C]): r = rm else: l = lm ans = sum([abs((r+l)//2 - c) for c in C]) print(ans)