import Control.Monad

main :: IO ()
main = do
    [n, c] <- map ( read :: String -> Integer) . words <$> getLine
    input <- replicateM (fromIntegral n :: Int) getLine
    let
        sushiListRaw = map (map (read :: String -> Integer) . words ) input
        tableInit = map listToSushi sushiListRaw

    print $ maxCalorie c tableInit

data Sushi = Sushi { x :: Integer, v :: Integer}
    deriving(Show, Eq)

type Table = [Sushi]

type Circumference = Integer

type Calorie = Integer

type Distance = Sushi -> Integer

listToSushi :: [Integer] -> Sushi
listToSushi xs = Sushi { x = head xs, v = xs !! 1 }

distance :: Circumference -> Sushi -> Integer
distance c sushi = min (x sushi) (c - x sushi)

getCalorie :: Circumference -> Sushi -> Integer
getCalorie c sushi = v sushi - distanceC sushi
    where distanceC = distance c

move :: Circumference -> Table -> Sushi -> Table
move c table sushi = map ( movePerSushi xMoved ) $ filter (/= sushi) table
    where
        xMoved = x sushi
        movePerSushi dx s = Sushi { x = ( c + x s - dx ) `mod` c, v = v s }

maxCalorie :: Circumference -> Table -> Integer
maxCalorie _ [] = 0
maxCalorie c [sushi] = max 0 $ getCalorie c sushi
maxCalorie c tbl = maximum $ zipWith (+) calorieChoices tableChoices
    where
        calorieChoices = map ( getCalorie c ) tbl
        tableChoices = map ( maxCalorie c . move c tbl ) tbl
