Time limit
2s
Memory limit
128 MB
A traveler wants to transfer trains from station 0 to station N.
Each train leaves station 0 at its listed minute, and the same departure repeats every hour. Moving between adjacent stations takes 1 minute, so a train that leaves station 0 at minute d passes station k at minute d + k, using the 60-minute cycle.
At time M, the traveler is at station 0. They take the earliest train they can board and get off at station 1. At each following station, they again wait for the earliest arriving train and ride it to the next station. They do not stay on the same physical train for two or more consecutive segments. Even if two trains have the same number, different departure minutes mean they are different trains.
Print the number of the train used on the final segment when the traveler arrives at station N.
The first line contains the number of train types T (T < 30), the traveler's starting time M in minutes (M < 60), and the destination station number N (N < 1,000,000,000).
Each of the next T lines contains a train number followed by the minutes when that train leaves station 0. A train number is a string of length at most 10. Every departure minute is an integer less than 60, and each line ends with -1. No two trains leave station 0 at the same minute.
Print the number of the train used on the final segment.