local n, k = io.read("*n", "*n")
local t, r = {}, {}
for i = 1, n do
  t[i] = io.read("*n")
  r[i] = 0
end
local abs, min = math.abs, math.min
r[2] = abs(t[2] - t[1])
for i = 2, n do
  r[i] = r[i - 1] + abs(t[i] - t[i - 1])
  for j = 2, k do
    if i <= j then break end
    r[i] = min(r[i], r[i - j] + abs(t[i] - t[i - j]))
  end
end
print(r[n])
