Stop & Go
면접 대비시간 제한2초메모리 제한1024 MB
자동차가 속도 1로 주행하며 각 신호등에서 멈출 수 있을 때, 도로 끝에 도착하는 시각을 구합니다.
문제
Mr. Drive, a.k.a. Mr. D, is famous for his thorough safe driving. Not only he always drives a car at an exact legal speed, but also he immediately stops a car if a traffic light turns red from green when he just enters a crossing, and he immediately starts a car at an exact legal speed when a traffic light just turns green from red.
Mr. D's next driving course is a simple straight road with length and the legal speed limit per second. Mr.D will start his drive at time . The road has traffic lights numbered through . The traffic light is at a distance of from the start point. At time , all the traffic lights are green. The -th traffic light turns red from green after seconds, then turns green from red after , and then turns red from green after seconds, then turns green from red after , and so on.
In this situation, Mr. D will start from the start point and run a car at speed per second. If the -th traffic light is green or just turns green from red (but not just turns red from green) when Mr. D reaches , Mr. D won't stop and go through the crossing at speed per second. If the -th traffic light is red or just turns red from green (but not just turns green from red) when Mr. D reaches , Mr. D will stop until the -th traffic light turns green.
Your task is to compute the time in seconds when Mr. D reaches point , for given traffic lights.
입력
The first line of the input consists of two integers, the number () of traffic lights on the road and the length () of the road. The -th of the following lines has three integers , , and , where () is the position of the -th traffic light from the start point, () is the duration the -th traffic light is green, and () is the duration the -th traffic light is red. You can assume all the positions of the traffic lights are different. In other words, holds for all .
출력
Output in a line a single integer, which is the time in seconds when Mr. D reaches point .