b = gets.to_i n = gets.to_i c = STDIN.map(&:to_i) c_uniq = c.uniq ans = [] c_uniq.each{|standard| give = b sum = 0 c.each{|candy| tmp = candy - standard give += tmp sum += tmp.abs } if give >= 0 ans.push(sum) end } p ans.min