import Data.List
import Control.Applicative

main = do
  [n, m] <- fmap read . words <$> getLine
  costs <- sort . fmap ((`max` 0).(n-).read.head.words) . lines <$> getContents
  print $ sum $ take (m-1) costs
