## Cheerio Contest 1 S1/J4 - Going to School

View as PDF

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 512M

Author:
Problem type

Bob is getting ready to walk to RHHS for the first day of school! The route from Bob's home to RHHS can be modelled as a number line with his house located at point and RHHS located at point . To get to school, however, he must cross signalled intersections, each located at distinct integer points (excluding point and point ) and cycles between red and green. The light is located at point and stays red for seconds and stays green for seconds.

At , all the traffic lights have just turned red, and Bob has just begun his walk, travelling unit per second towards the school. Since Bob is a law-abiding citizen, he will only cross an intersection if the light is green. Otherwise, he will wait at the intersection until the light turns green. Bob knows the school is quite far away, so he wants to know the total time he will take to get to school. Can you write a program to help him?

Points Awarded
5 points
5 points and
5 points

#### Input Specification

The first line contains two integers and .

The next lines each contain three integers , , and .

#### Output Specification

Output the number of seconds Bob will take to get to school. Please note that this number may not fit inside a 32-bit integer.

3 25
6 28 6
15 15 22
19 4 9

62

#### Explanation for Sample Output

At , Bob is at point , and all the traffic lights have just turned red. When he arrives at point , the traffic light is red, so he is forced to wait. At , he can continue.

When he arrives at point , the light has just turned red, so he is forced to wait again. A , he resumes his walk. When he reaches point , the light has just turned green, so he can continue walking as usual. He arrives at RHHS at .