Metro
Algorithms
Graphs
Medium
Shortest path algorithm
82.0% success
930 attempts
2s time
256MB memory
1024KB max code

A city subway line has become huge and it is hard to take the shortest path through them. You have to find the shortest path in subway lines. In the second \(0\), you are in the station \(start\) and you want to go to the station \(end\).

The city has \(n\) stations. The subway has \(m\) lines. Each subway line goes to some stations.

The $$i-th$$ subway goes to stations \(u_{i, 1}, u_{i, 2}, u_{i, 3}, ..., u_{i, k_i}\)(in order) and this train takes $$w_{i, j}$$ seconds to travel from \(u_{i, j}\) to \(u_{i, j+1}\)(for \(1 \le i \le m\) and \(1 \le j < k_i\)).

Trains are ready for the passengers to get in, but the last train goes in the second $$t_i$$(and you are allowed to board it in between the path).


Input 

The first line contains $$n, m$$(in order).

Next $$3*m$$ lines describe subway lines.

The first line contains $$k_i, t_i$$ and next line contains \(u_{i, 1}, u_{i, 2}, u_{i, 3}, ..., u_{i, k_i}\)and the next line contains \(w_1, w_2, ..., w_{k_{i - 1}}\).

The last line containts $$start, end$$.

It is guaranteed \(start \neq end\) and no subway line intersects itself.


Output 

Print the shortest path in subway lines from the station \(start\) to \(end\).

If there is no way from \(start\) to \(end\), print -1.

 

Constraints

\(1 \le n, m \le 100\ 000\)

\(1 \le u_{i, j} \le n\)

\(2 \le k_i\)

\(k_1 + k_2 + ... + k_m \le 100\ 000\)

\(0 \le t_i \le 10^9\)

\(1 \le w_{i, j} \le 10^9\)

 

Examples
Input
4 2
2 0
1 2
1
3 0
3 2 4
1 1
1 4
Output
2
Explanation

In second $$0$$ get on first subway line and in next second we arrive to station $$2$$ and we can get on second line and arrive to $$4$$ in second $$2$$

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading Editor...