This page is still under construction.

Parts of this page are still being built. What you see may change.

Transfer Train

Time limit8sMemory limit512 MB

Summary
Find a route from station A to B minimizing total ride time, breaking ties by fewest transfers, where each line can be ridden in either direction and repeated station names on one line require a transfer.
Level

Medium7 of 10

Topics
Graph, Shortest path, Hash map, Greedy
Solved
No attempts yet

Problem

Eel likes riding trains. Right now Eel is trying to travel from station A to station B by train. Eel is in a hurry, so Eel decided to take a route with the shortest travel time. However, Eel is not good at transferring, so if there are several routes with the shortest travel time, Eel decided to take the route with the fewest transfers.

There are NN lines. The ii-th line passes through aia_i stations. The names of the stations the ii-th line passes through, in order, are si,0,...,si,ai−1s_{i,0}, ... , s_{i,a_i -1}, and the travel times between consecutive stations are ti,0,...,ti,ai−2t_{i,0}, ..., t_{i,a_i - 2}. Trains run along a line in both directions, so you can also ride the stations in the reverse of the order given in the input. The same station name on multiple lines denotes the same station, and you can transfer between them. A transfer takes TT minutes regardless of the station or line.

A single line may pass through the same station more than once. If you want to move between two stations that have the same name and are on the same line but at different positions within the line, you must transfer. For example, when using the line C - D - E - F - D - G to go from C to G, you can ride a single train from the start to the end, or you can transfer at station D and ride C - D and D - G separately.

Find the time and the number of transfers Eel needs to travel from station A to station B. Trains come very frequently, so waiting time can be ignored.

Input

The input is given in the following format:

NN TT

AA BB

a1a_1

s1,1s_{1,1} ... s1,a1s_{1,a_1}

t1,1t_{1,1} ... t1,a1−1t_{1,a_1 -1}

...

aNa_N

sN,1s_{N,1} ... sN,aNs_{N,a_N}

tN,1t_{N,1} ... tN,aN−1t_{N,a_N-1}

Output

Print the time Eel needs to travel from station A to station B and the number of transfers, separated by a space, on one line.

Constraints

  • NN will be between 1 and 50,000, inclusive.
  • TT will be an integer between 1 and 1,000, inclusive.
  • At least one train stops at A.
  • At least one train stops at B.
  • A and B will be distinct.
  • aia_i will be bigger than or equal to 2.
  • a1+...+aNa_1 + ... + a_N will be between 2 and 100,000, inclusive.
  • si,js_{i,j} will contain between 1 and 10 characters, inclusive.
  • Each character in si,js_{i,j} will be a letter ('A'-'Z', 'a'-'z').
  • ti,jt_{i,j} will be an integer between 1 and 1,000, inclusive.

Examples2

  1. Example 1

    Input
    2 10
    Warsaw Petersburg
    3
    Kiev Moscow Petersburg
    150 120
    3
    Moscow Minsk Warsaw
    100 150
    
    Expected output
    380 1
    
  2. Example 2

    Input
    2 10
    Warsaw Petersburg
    3
    Kiev Moscow Petersburg
    150 120
    2
    Minsk Warsaw
    150
    
    Expected output
    -1