{-# LANGUAGE FlexibleInstances #-}

comb :: Int -> [Int] -> [[Int]]
comb 0 xs = [[]]
comb _ [] = []
comb n (x:xs) = [x:y | y <- comb (n-1) xs] ++ comb n xs


divide :: [Int] -> (Int, Int, [Int])                              -- [7, 4, 2, 6, 4] -> (1, 7, [4, 2, 6, 4]), [7, 8, 2, 6, 4] -> (2, 8, [2, 6, 4])
divide (x:xs) = f xs x 1
  where
    f :: [Int] -> Int -> Int -> (Int, Int, [Int])
    f (x:xs) last n = if last < x then f xs x (n + 1) else (n, last, x:xs)
    f [] last n = (n, last, [])


buildUp :: Int -> [Int] -> ([Int], [Int])
buildUp last ns = (f ns last [], ns)
  where
    f :: [Int] -> Int -> [Int] -> [Int]
    f [] _ acc = acc
    f (n:ns) last acc = f ns fixed $ acc ++ [fixed]
      where
        fixed = max (last + 1) n


cost :: ([Int], [Int]) -> Int
cost (xs, ys) = sum $ map (\(x, y) -> x - y) $ zip xs ys


class In a where stdin :: IO a
instance In [Int] where stdin = map read . words <$> getLine

main = do
    [n, k] <- stdin
    as <- stdin

    let (visibleN, lastBuilding, invisibles) = divide as         -- [7, 4, 2, 6, 4]
    let remainN = k - visibleN

    let buildss = comb remainN invisibles                        -- [[4,2],[4,6],[4,4],[2,6],[2,4],[6,4]]

    let pairs = map (\bs -> buildUp lastBuilding bs) buildss     -- [([8,9],[4,2]),([8,9],[4,6]),([8,9],[4,4]),([8,9],[2,6]),([8,9],[2,4]),([8,9],[6,4])]

    print $ minimum $ map cost pairs
