import Control.Monad

solve :: Int -> [Int] -> Int -> Int -> Int
solve 0 as hd cnt = cnt
solve k [] _ _ = 10^14 
solve k (a:as) hd cnt = minimum [left, right]
  where
    left = 
      if hd+1 >= a then solve (k-1) as (hd+1) (cnt + (hd-a+1)) 
      else solve (k-1) as a cnt 
    right = solve k as hd cnt



main = do
  [n,k] <- map read . words <$> getLine
  a <-  map read . words <$> getLine :: IO [Int]

  let ans = solve (k-1) (tail a) (head a) 0
  print ans