require 'pp'

N,K = readline.split.map(&:to_i)
H = readline.split.map(&:to_i)

dp = Array.new(N,0)

dp[0] = 0

(1..(N-1)).each do |i|
  k = (i<K) ? i : K
  costs = (1..k).each.map {|k|
    dp[i-k] + (H[i-k]-H[i]).abs
  }
  dp[i] = costs.min
end

puts dp.last
