# array = [*1..q].map { |_| gets.split.map(&:to_i) }
# n = gets.split.map(&:to_i)
# a = [*1..m].repeated_combination(n).to_a
 def chmin(a, b) a < b ? a : b end
# def chmax(a, b) a > b ? a : b end
INF = Float::INFINITY
#
n,k = gets.split.map(&:to_i)
h = gets.split.map(&:to_i)

dp = [INF] * (n+1+k)

dp[0] = 0
0.upto(n-2) do |i|
  if i == 0
    dp[i+1] = (h[i+1] - h[i]).abs
  else
    1.upto(k) do |j|
      dp[i+1] = chmin(dp[i+1] , dp[i+1-j] + (h[i+1-j] - h[i+1]).abs) if i+1 - j >= 0
    end
    # i = 3 
    # dp[3+1] = chmin(dp[3+1], dp[3] + 3 - 4)
    # dp[3+1] = chmin(dp[3+1], dp[2] + 2 - 4)
    # dp[3+1] = chmin(dp[3+1], dp[1] + 1 - 4)
  end
end

puts dp[n-1]
