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

jump = Array.new(n).map{ Array.new(n) }

hs.each_with_index{ |h, i|
	(i + 1 ... hs.size).each{ |j|
		diff = (hs[i] - hs[j]).abs
		jump[i][j] = diff
		jump[j][i] = diff
	}
}

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] + jump[j][i]
		cost[i] = next_cost if cost[i] > next_cost
	}
}

puts cost[n-1]