n, k = gets.chomp.split(" ").map(&:to_i)
h = gets.chomp.split(" ").map(&:to_i)
dp = Array.new(n,Float::INFINITY)
dp[0] = 0
# p dp
0.upto (n-1) do |i|
    if i - k < 0
    chmin = (h[i] - h[[i-k,0].max]).abs
    else
        chmin = (h[i] - h[i-k]).abs + dp[i-k] 
    end
    # p i - k
    # p h[i] - h[[i-k,0].max]
    # p dp
    if i >= 2
        # p [i-k,1].max
        # p i-1
        ([i-k,1].max).upto (i-1) do |j|
            #p (dp[j]+ (h[i] - h[j]).abs).abs
            chmin = [chmin,(dp[j]+ (h[i] - h[j]).abs).abs].min
        end
    end
    dp[i] = chmin
    # puts "iiiiiiiiiiii"
  
end
puts dp.pop