import Control.Applicative import Control.Monad import Data.List main :: IO () main = do [n, l] <- f solve l <$> replicateM n f >>= print where f = map read <$> words <$> getLine solve :: Int -> [[Int]] -> Int solve l = g . foldl' f (0, 0) where f (x, t) [xi, wi, ti] = let st = t + xi - x (q, r) = st `divMod` ti in if odd q then (xi + wi, st + ti - r + wi) else if ti - r >= wi then (xi + wi, st + wi) else (xi + wi, st + 2 * ti - r + wi) g (x, t) = t + l - x