def func(n, candy_list, b): ans = 0 for c in candy_list: if c > n: d = c - n ans += d b += d elif c < n: d = n - c if d > b: return 10 ** 15 ans += d b -= d return ans def main(): b = int(input()) n = int(input()) candy_list = sorted([int(input()) for _ in range(n)], reverse=True) ans = 10 ** 15 for x in set(candy_list): ans = min(ans, func(x, candy_list, b)) print(ans) if __name__ == '__main__': main()