b = gets.to_i n = gets.to_i c = STDIN.map(&:to_i) ans = [] n.times{|i| standard = c[i] ans.push(c.map{|candy|(candy - standard).abs}.inject(&:+)) } p ans.min