
import Data.Array (Array, listArray, (!))

dp f s e = result ! e where
  result = listArray (s,e) $ map nth [s..]
  nth i = f result i

minjumpcost :: Int -> Int -> Array Int Int -> Int
minjumpcost n k hs = dp f 1 n where
  f _ 1 = 0
  f t i =
    let costs = [(t!j) + abs ((hs!i)-(hs!j)) | j <- [i-1,i-2..i-k], j>0]
    in
      minimum costs

main = do
  [n,k] <- map read . words <$> getLine
  hs <- listArray (1,n) . map read . words <$> getLine
  print $ minjumpcost n k hs
