Travel Plan (Small)

No attempts yetTime limit1sMemory limit128 MB

Problem

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,,n1, 2, \ldots, n, correspond to stations, and an edge (pi,pj)(p_i, p_j) with pipjp_i \ne p_j means there is a direct connection between stations pip_i and pjp_j (1pi,pjn1 \le p_i, p_j \le n).

Transportation lines are numbered 1,2,,k1, 2, \ldots, k. Line ll is defined by the sequence of stations pl,1,pl,2,,pl,slp_{l,1}, p_{l,2}, \ldots, p_{l,s_l} where its vehicles stop, together with the travel times rl,1,rl,2,,rl,sl1r_{l,1}, r_{l,2}, \ldots, r_{l,s_l-1} between consecutive stations: rl,1r_{l,1} is the time to go from station pl,1p_{l,1} to pl,2p_{l,2} (or back), rl,2r_{l,2} is the time from pl,2p_{l,2} to pl,3p_{l,3}, and so on. All stations on one line are distinct (that is, iji \ne j implies pl,ipl,jp_{l,i} \ne p_{l,j}).

Vehicles on line ll run with a fixed frequency clc_l, where clc_l belongs to the set {6,10,12,15,20,30,60}\{6, 10, 12, 15, 20, 30, 60\}. Vehicles depart station pl,1p_{l,1} at the top of every hour throughout the whole day (an hour mark being minute 00 of hour gg with 0g230 \le g \le 23), and then, following the frequency, at clc_l minutes, 2cl2c_l minutes, and so on after each hour mark. Vehicles of line ll run in both directions, from pl,1p_{l,1} toward pl,slp_{l,s_l} and from pl,slp_{l,s_l} toward pl,1p_{l,1}. The departure times from pl,slp_{l,s_l} are the same as from pl,1p_{l,1}.

In this network we want to travel from a start station xx to a finish station yy. 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 00 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 yy from the start station xx as early as possible.

For example, the picture below shows a network with 66 stations and two lines, 11 and 22. Vehicles of line 11 run between stations 1,3,4,61, 3, 4, 6, and vehicles of line 22 run between stations 2,4,3,52, 4, 3, 5. The frequencies are c1=15c_1 = 15 and c2=20c_2 = 20. The travel times between stations are written next to the edges, with subscripts 11 and 22 to indicate the line.

Suppose that at 23:30 you are at station 55 and want to reach station 66. You wait 1010 minutes and then board line 22 at 23:40. There are two options. In the first, you reach station 33 at 23:51, wait 33 minutes, change to line 11 at 23:54, and arrive at station 66 at 0:16 the next day. In the second, you stay on line 22 and reach station 44 at 0:08, wait 1313 minutes, board line 11 at 0:21, and arrive at station 66 at 0:31. Hence the earliest time to reach station 66 is 0:16.

Write a program that:

  • reads from standard input the transportation network, the lines, the start station number xx, the finish station number yy, and the hour and minute of the beginning of the trip, gxg_x and mxm_x;
  • finds the shortest travel time from the start station xx to the finish station yy;
  • writes to standard output the earliest possible arrival time at the finish station yy, that is the hour gyg_y and minute mym_y.

Input

The first line of standard input contains six integers separated by single spaces:

  • the number of stations nn (1n1,0001 \le n \le 1{,}000),
  • the number of lines kk (1k2,0001 \le k \le 2{,}000),
  • the start station number xx (1xn1 \le x \le n),
  • the finish station number yy (1yn1 \le y \le n),
  • the hour of the beginning of the trip gxg_x (0gx230 \le g_x \le 23),
  • the minute of the beginning of the trip mxm_x (0mx590 \le m_x \le 59).

Stations are numbered from 11 to nn, and lines from 11 to kk. The following 3k3k lines describe the lines; each description takes three consecutive lines.

  • The first line describing line ll contains two integers: sls_l, the number of stations (2sln2 \le s_l \le n), and clc_l, the frequency (cl{6,10,12,15,20,30,60}c_l \in \{6, 10, 12, 15, 20, 30, 60\}).
  • The second line describing line ll contains the sls_l distinct integers pl,1,pl,2,,pl,slp_{l,1}, p_{l,2}, \ldots, p_{l,s_l}, the numbers of the consecutive stations on line ll (1pl,in1 \le p_{l,i} \le n).
  • The third line describing line ll contains the sl1s_l - 1 integers rl,1,rl,2,,rl,sl1r_{l,1}, r_{l,2}, \ldots, r_{l,s_l-1}, the times in minutes to travel between consecutive stations of the line (1rl,i2401 \le r_{l,i} \le 240).

The total number of stations over all lines is at most 4,0004{,}000 (that is, s1+s2++sk4,000s_1 + s_2 + \cdots + s_k \le 4{,}000).

Output

Print a single line with two integers separated by a single space: the hour gyg_y (0gy230 \le g_y \le 23) and the minute mym_y (0my590 \le m_y \le 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).