def min2(a, b)
    return a <= b ? a : b
end

n, k = gets.chomp.split(" ").map(&:to_i)
h = gets.chomp.split(" ").map(&:to_i)

# cost[i] : i-1段目到達の最小コスト
cost = Array.new(h.length+1, Float::INFINITY)
cost[0] = 0

(1..(n-1)).each do |i|
    (1..k).each do |j|
        break if i < j
        cost[i] = min2(cost[i], cost[i-j] + (h[i]-h[i-j]).abs)
    end
end

# (0..n-1).each do |i|
#     puts "#{i} : #{cost[i]}"
# end
puts cost[n-1]	