import qualified Data.ByteString.Char8 as C
import Data.Maybe

main = sol <$> get <*> get >>= print

get = fmap fst . mapMaybe C.readInt . C.words <$> C.getLine

sol [_,x] as = pm (s,head bs) (tail bs)
  where
  bs = map (min x) as
  s = sum . map (subtract x) $ filter (>x) as
  pm (s,p) []   = s
  pm (s,p) (a:as)
    | p+a > x   = pm (s+x,(p+a) `mod` x) as
    | otherwise = pm (s,a) as
