import Control.Monad
import Control.Monad.Trans.State.Strict
import Data.Char (isSpace)
import Data.Maybe (fromJust)
import Data.List
import qualified Data.ByteString.Lazy.Char8 as B
--
type Parser a = StateT B.ByteString Maybe a

parseInt :: Parser Int
parseInt = StateT (B.readInt . B.dropWhile isSpace)

parseInt2 :: Parser (Int,Int)
parseInt2 = liftM2 (,) parseInt parseInt

parse :: Parser (Int,Int, [(Int,Int)])
parse = do
  (n,c)<-liftM2 (,) parseInt parseInt
  xv<-replicateM n parseInt2
  return (n,c,xv)

main=do
  (n,c,xv) <- fromJust.evalStateT parse <$> B.getContents
  print $ sol n c xv
--
sol::Int->Int->[(Int,Int)]->Int
sol n c xv = max cw cc
  where
    (cwm,cwv) = unzip $ scanl (\(m,(_,a)) (x,v)->(max m (a+v-x), (x,a+v))) (0,(0,0)) xv
    (ccm,ccv) = unzip $ scanl (\(m,(_,a)) (x,v)->(max m (a+v-(c-x)), (c-x,a+v))) (0,(0,0)) $ reverse xv
    cw = maximum.map (\((x,v),m)-> m + v-2*x). zip ccv $ reverse cwm
    cc = maximum.map (\((x,v),m)-> m + v-2*x). zip cwv $ reverse ccm

