n, k = gets.split.map &:to_i
h = gets.split.map &:to_i

cost = Array.new(n, 0)
cost[0] = 0

1.upto(n-1).each{|i|
    c = 1e8
    1.upto(k).each{|j|
        if i - j >= 0
            c_ = (h[i] - h[i-j]).abs + cost[i-j]
            c = c_ if c_ < c
        end
    }
    cost[i] = c
}

puts cost.last
