main = do [_,l] <- map read . words <$> getLine xwts <- map (map read . words) . lines <$> getContents print (crossing l xwts) crossing :: Int -> [[Int]] -> Int crossing l = rest . foldl cross (0,0) where rest (t,d) = t + (l-d) cross (t',d') xwt@[x,w,t] | d' /= x = cross (t'+(x-d'),x) xwt | mod t' (2*t) >= t = cross (next,d') xwt | t - mod t' (2*t) < w = cross (next,d') xwt | otherwise = (t'+w,d'+w) where next = head (dropWhile (