N, K = gets.split(" ").map(&:to_i)
H = gets.split(" ").map(&:to_i)
dp = []
dp[0] = 0
(1...N).each{|i|
    min = Float::INFINITY
    (1..K).each{ |j|
        if i - j >= 0
            cost  = dp[i-j] + (H[i-j] - H[i]).abs
            min = cost if min > cost
        end
    }
    dp[i] = min
}
puts dp[N-1]