Consider a diagram of a public transportation network, for example a bus network, tram network, or underground network. The vertices of the diagram, numbered 1,2,…,n, correspond to stations, and an edge (pi,pj) with pi=pj means there is a direct connection between stations pi and pj (1≤pi,pj≤n).
Transportation lines are numbered 1,2,…,k. Line l is defined by the sequence of stations pl,1,pl,2,…,pl,sl where its vehicles stop, together with the travel times rl,1,rl,2,…,rl,sl−1 between consecutive stations: rl,1 is the time to go from station pl,1 to pl,2 (or back), rl,2 is the time from pl,2 to pl,3, and so on. All stations on one line are distinct (that is, i=j implies pl,i=pl,j).
Vehicles on line l run with a fixed frequency cl, where cl belongs to the set {6,10,12,15,20,30,60}. Vehicles depart station pl,1 at the top of every hour throughout the whole day (an hour mark being minute 0 of hour g with 0≤g≤23), and then, following the frequency, at cl minutes, 2cl minutes, and so on after each hour mark. Vehicles of line l run in both directions, from pl,1 toward pl,sl and from pl,sl toward pl,1. The departure times from pl,sl are the same as from pl,1.
In this network we want to travel from a start station x to a finish station y. Assume the trip is always possible and takes no longer than 24 hours. During the trip you may change lines as many times as you like. A change itself takes 0 time, but you must account for the time spent waiting for the vehicle you want to board. The goal is to reach the finish station y from the start station x as early as possible.
For example, the picture below shows a network with 6 stations and two lines, 1 and 2. Vehicles of line 1 run between stations 1,3,4,6, and vehicles of line 2 run between stations 2,4,3,5. The frequencies are c1=15 and c2=20. The travel times between stations are written next to the edges, with subscripts 1 and 2 to indicate the line.

Suppose that at 23:30 you are at station 5 and want to reach station 6. You wait 10 minutes and then board line 2 at 23:40. There are two options. In the first, you reach station 3 at 23:51, wait 3 minutes, change to line 1 at 23:54, and arrive at station 6 at 0:16 the next day. In the second, you stay on line 2 and reach station 4 at 0:08, wait 13 minutes, board line 1 at 0:21, and arrive at station 6 at 0:31. Hence the earliest time to reach station 6 is 0:16.
Write a program that:
The first line of standard input contains six integers separated by single spaces:
Stations are numbered from 1 to n, and lines from 1 to k. The following 3k lines describe the lines; each description takes three consecutive lines.
The total number of stations over all lines is at most 4,000 (that is, s1+s2+⋯+sk≤4,000).
Print a single line with two integers separated by a single space: the hour gy (0≤gy≤23) and the minute my (0≤my≤59) of the earliest possible arrival at the finish station. If the arrival falls on the next day, print the clock time taken modulo one full day (24 hours).