n,k = gets.split.map &:to_i
hs = gets.split.map &:to_i

cost = Array.new(n + 1, Float::INFINITY)

cost[0] = 0

(1...n).each{ |i|
	start = i - k < 0 ? 0 : i - k
	
	(start ... i).each{ |j|
		next_cost = cost[j] + (hs[j] - hs[i]).abs
		cost[i] = next_cost if cost[i] > next_cost
	}
}

puts cost[n-1]