import Data.Array
import Data.Array.ST
import Control.Monad
import Control.Monad.ST

gen_dp_tbl::Array Int Int ->Int->Int->Array Int Int
gen_dp_tbl ar n k= runSTArray $ do
  tbl<-newArray_ (1,n)
  forM_ [1..(min k n)] (\ i -> writeArray tbl i (abs (ar!1 - ar!i)))
  forM_ [(k+1)..n] (\ i -> do
                      l<-forM [i-k..i-1] (\ j -> readArray tbl j)
                      let next = minimum (map (\(x,y)->x+y) (zip l [abs (ar!j  - ar!i)|j<-[i-k..i-1]]))
                      writeArray tbl i next)
  return tbl

solver::[Int]->Int->Int->Int
solver ls n k= (gen_dp_tbl (listArray (1,n) ls) n k)!n

main::IO()
main = do
   (n:k:_)<-(return.(map read).words)=<<getLine
   ls<-(return.(map read).words)=<<getLine
   print (solver ls n k)