#!/usr/bin/env ruby

(n, k) = gets.chomp.split(/ /).map(&:to_i)
#5, 3
h = gets.chomp.split(/ /).map(&:to_i)

dp = []
# dp[n] n番目に到達する最小値
# dp[n] = [dp[n-1] + h, dp[n - 2] + h], ...dp[n - k] + h].min
(1..(n - 1)).each do |x|
  dp[x] = 9999999
end
dp[0] = 0
(1..(n - 1)).each do |i|
  (1..k).each do |j|
    if (i - j >= 0)
      dp[i] = [dp[i], dp[i - j] + (h[i] - h[i - j]).abs].min
    end
  end
end
#p dp
puts dp[n - 1]
