Bus
Time limit1sMemory limit2048 MB
Find the shortest travel time from Zagreb to Wroclaw by taking a Zagreb-Graz bus, then a Graz-Wroclaw bus, or report NEMOGUCE if no connection works.
- Level
Easy3 of 10
- Topics
- Brute force, Implementation
- Solved
- No attempts yet
Problem
Mr. Malnar decided to visit Wroclaw (Pol. Wrocław), a city in southwest Poland that he has not yet visited. He had not traveled by bus for a long time and missed the experience, but he found that there is no direct bus line between Zagreb and Wroclaw.
The best option is to transfer in the Austrian city of Graz. Mr. Malnar found a timetable listing the bus lines on the routes Zagreb-Graz and Graz-Wroclaw. A bus on a given route runs daily. It departs exactly at the start of the departure minute and arrives exactly at the end of the arrival minute. Transfer time is negligible: you can board a bus if you arrive before the bus you want departs. In other words, the arrival time of the first bus must be strictly less than the departure time of the second bus.
Determine the shortest time required to travel from Zagreb to Wroclaw.
Input
The first line contains a positive integer (), the number of bus lines.
Each of the next lines contains the names of two cities joined by -, the departure city first and the destination second, followed by the departure time and arrival time in the format h:mm--h:mm. Here h is the hour and mm is the minute, always written with two digits, with a leading zero when needed. Each trip without transfers lasts at most 24 hours.
Output
If a trip from Zagreb to Wroclaw is possible, print the travel time on the first line in the format h:mm.
Constraints
If the trip is not possible, print NEMOGUCE on the first line.