import Control.Monad

f now c [] = 0
f now c ms = maximum [goRight now c ms, goLeft now c ms, 0]

goRight _ _ [] = 0
goRight now c (m:ms) = cal - dist + f (head m) c ms
  where
    dist = (head m + c - now) `mod` c
    cal = last m

goLeft _ _ [] = 0
goLeft now c ms = cal - dist + f (head sara) c (init ms)
  where
    sara = last ms
    dist = (now + c - head sara) `mod` c
    cal = last sara

main = do
  [n,c] <- (map read . words) <$> getLine
  ms <- map (map read . words) <$> replicateM n getLine
  print [n,c]
  print ms
  print $ f 0 c ms
