B=int(input()) N=int(input()) C=[int(input()) for i in range(N)] x=sum(C)//N ANS=1<<100 for xx in C: if xx*N>sum(C)+B: i=(sum(C)+B)//N else: i=xx score=0 for c in C: score+=abs(i-c) ANS=min(ANS,score) print(ANS)