#t517. 最优乘车
最优乘车
Optimal Bus Ride
Problem Background
Special for beginners, ^_^
Problem Description
City is a tourist resort that attracts thousands of visitors every year. For the convenience of tourists, the bus company has set up bus stops at various tourist attractions, hotels and restaurants, and opened some one-way bus lines. Each one-way bus line starts from a bus stop, passes through several stops in order, and finally reaches the terminal stop.
A tourist recently came to visit city . He really wants to go to Park, but if there is no single bus line from his hotel directly to Park, he may first take one bus line for a few stops, then get off and transfer to another line at the same stop, and after a few such transfers finally reach Park.
Now number all the bus stops in city with integers . It is agreed that the bus stop of the hotel where the tourist stays is numbered , and the bus stop of Park is numbered .
Write a program to help this tourist find an optimal riding plan that minimizes the number of transfers he makes on the way from the hotel to Park.
Input Format
The first line of input contains two numbers and (, ), meaning one-way bus lines are open and there are stations in total.
From the second line to the -th line, the information of the -st to the -th bus line is given in order. The -th line gives the information of the -th bus line: all the stop numbers on that line in running order, separated by a single space.
Output Format
Output only one line: if it is impossible to reach Park from the hotel by bus, output NO; otherwise output the minimum number of transfers found by your program. A transfer count of means no transfer is needed to arrive.
Sample Input
3 7
6 7
4 7 3 6
2 1 3 5
Sample Output
2
Constraints
Time limit: 1s; memory limit: 1024 KiB for each test case.