module Main where

import Data.List
--import Data.Array
import Data.Array.Unboxed as U --Immutable
import Data.Array.ST
import Data.Array.MArray
import Control.Monad
import Control.Monad.ST

--k>=1
dp ::  UArray Int Int -> STUArray s Int Int -> Int -> Int -> ST s Int
dp hs starr 1 k = return 0
dp hs starr i k = do
  let nowh = hs!i
  nexts <- forM [max 1 (i-k)..i-1] $ \j -> do
    dp_j <- readArray starr j
    return $ dp_j+abs(nowh-hs!j)
  return $ minimum $ nexts

--dpをどう動かすか
solve :: UArray Int Int -> Int -> Int -> Int
solve hs n k = runST $ do
  starr <- newArray (1,n) 0 :: ST s (STUArray s Int Int)
  forM_ [1..n] $ \i -> dp hs starr i k >>= writeArray starr i
  readArray starr n

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