{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Data.List

main = do
  [n,c] <- fmap (map read . words) getLine
  !text <- getContents
  let xs = map ((\(x:v:_)-> x`seq`v`seq`(x,v)) . map read . words) (lines text)
  print (solve c xs)


-- MaxState, CurrentState, CurrentX
type State = (Int, (Int, Int))

solve :: Int -> [(Int,Int)] -> Int
solve c xs =
  solveTurn roundR roundL
  where
    !roundR = scanl f (0,(0,0)) xs
    !roundL = scanr (flip f) (0,(0,0)) [(c-x,v)|(x,v)<-xs]

--    f (m,(s,y)) (x,v) = let s' = s-(x-y)+v in s' `seq` (max m s', (s', x))
    f (m,(s,y)) (x,v) = let s' = s-(x-y)+v in (max m s', (s', x))

solveTurn :: [State] -> [State] -> Int
solveTurn ss1 ss2 =
--  go ss1 ss2 0
  foldl' f 0 (zip ss1 ss2)
  where
--    go :: [State] -> [State] -> Int -> Int
--    go [(m1,(s1,x1))]     [(m2,(s2,x2))]     a = max a (max m1 m2)
--    go ((m1,(s1,x1)):tt1) ((m2,(s2,x2)):tt2) a = let a' = max a $! (s1+m2-x1) in a' `seq` go tt1 tt2 a'
--    go ((m1,(s1,x1)):tt1) ((m2,(s2,x2)):tt2) a = let a' = max a $! max (s1+m2-x1) $! (s2+m1-x2) in a' `seq` go tt1 tt2 a'

    f a (((m1,(s1,x1))),((m2,(s2,x2)))) = max a $! max (s1+m2-x1) $! (s2+m1-x2)