arr = gets.split.map(&:to_i)
ashi = gets.split.map(&:to_i)
dp = Array.new(arr[0], Float::INFINITY)
dp[0] = 0

(arr[0]-1).times do |i|
	arr[1].times do |j|
		if dp[i+1+j]
			dp[i+1+j] = [(ashi[i] - ashi[i+1+j]).abs + dp[i], dp[i+1+j]].min
		end
	end
end

p dp[arr[0]-1]