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 (max m s', (s', x))

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