import sys input=lambda: sys.stdin.readline().rstrip() b=int(input()) n=int(input()) C=[int(input()) for _ in range(n)] ans=float("inf") for c in C: ctp=0 ctm=0 for d in C: if d>c: ctp+=d-c else: ctm+=c-d if ctp+b>=ctm: cur=0 for e in C: cur+=abs(e-c) ans=min(ans,cur) print(ans)