{-# LANGUAGE BangPatterns #-}
import qualified Data.Vector.Unboxed as U
import qualified Data.ByteString.Char8 as C
import Data.List

main = do
    [n,k] <- unfoldr (C.readInt . C.dropWhile (==' ')) <$> C.getLine
    h <- U.unfoldrN n (C.readInt . C.dropWhile (==' ')) <$> C.getLine

    let dp = U.constructN n (\dp_ ->
            if U.null dp_ then 0
                else let !i = U.length dp_
                         !hi = h U.! i
                         f = U.slice (max 0 (i-k)) (min i k)
                         refh = f h
                         refd = f dp_
                    in U.minimum (
                        U.zipWith (\dc hc -> dc + abs(hi - hc)) refd refh
                        )
            )

    print $ U.last dp

