{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
-- import Data.List
import qualified Data.Vector.Unboxed as VU
-- import qualified Data.Vector.Unboxed.Mutable as VUM
-- import Control.Monad
-- import Control.Monad.ST
-- import Debug.Trace
-- trace _ = id

lim :: Int
lim = 10 ^ 10

solve :: Int -> Int -> [Int] -> Int
solve bigN bigK hs = sub 1 (VU.cons 0 (VU.replicate (bigK-1) lim))
  where
    vH = VU.fromListN (bigN+1) (0:hs)
    sub i vX
      | i == bigN = VU.head vX
      | otherwise =
          sub (i+1) (VU.zipWith f
                     (VU.drop (i+1) vH) (VU.snoc (VU.tail vX) lim))
      where f h x = min x ((VU.head vX) + abs (h - (vH VU.! i)))

readBInt :: B.ByteString -> Int
readBInt = fst . fromJust . B.readInt

tmain :: B.ByteString -> Int
tmain cont =
  let remLines0 = map B.words (B.lines cont)
      [bs_n, bs_k]:remLines1 = remLines0
      n = readBInt bs_n
      k = readBInt bs_k
      line2:remLines2 = remLines1
      hs = map readBInt line2
  in solve n k hs

outAnswer :: Int -> IO ()
outAnswer = putStrLn . show

main :: IO ()
main = outAnswer . tmain =<< B.getContents
