import qualified Data.ByteString.Char9 as B
import           Data.Graph.Inductive
import           Data.List

main :: IO ()
main = do
  [n, k] <- map read . words <$> getLine
  hs <- unfoldr (B.readInt . B.dropWhile (== ' ')) <$> B.getLine
  print $ solve n k hs

solve :: Int -> Int -> [Int] -> Int
solve n k hs = spLength 1 n gr
  where
    gr = mkGraph ns es :: Gr () Int
    ns = [ (i, ()) | i <- [1..n] ]
    es = [ (i, i+j, abs (h0-h1)) | j <- [1..k], (i,h0,h1) <- zip3 [1..] hs (drop j hs) ]
