local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
local function array(n, default_val)
    local metatables = {}
    if default_val ~= nil then
        metatables[1] = {__index = function() return default_val end}
    end
    for i=2,n do
        metatables[i] = {__index = function(parent, k)
            local child = setmetatable({}, metatables[i-1])
            rawset(parent, k, child)
            return child
        end}
    end
    return setmetatable({}, metatables[n])
end
----
local N, K = read.nn()
local h = read.N(N)
local function diff(i, j)
    if j > N then
        return 10^20
    end
    return math.abs(h[i] - h[j])
end
local tbl = array(1, 10^20)
tbl[1] = 0
for i=1,N-1 do
    for k=1,K do
        tbl[i+k] = math.min(tbl[i+k], diff(i,i+k) + tbl[i])
    end
end
print(tbl[N])