#t517. 最优乘车

最优乘车

Optimal Bus Ride

Problem Background

Special for beginners, ^_^

Problem Description

City HH 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 HH. He really wants to go to SS Park, but if there is no single bus line from his hotel directly to SS 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 SS Park.

Now number all the bus stops in city HH with integers 1,2,…,N1, 2, \dots, N. It is agreed that the bus stop of the hotel where the tourist stays is numbered 11, and the bus stop of SS Park is numbered NN.

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 SS Park.

Input Format

The first line of input contains two numbers MM and NN (1≤M≤1001 \le M \le 100, 1<N≤5001 < N \le 500), meaning MM one-way bus lines are open and there are NN stations in total.

From the second line to the MM-th line, the information of the 11-st to the MM-th bus line is given in order. The (i+1)(i+1)-th line gives the information of the ii-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 SS Park from the hotel by bus, output NO; otherwise output the minimum number of transfers found by your program. A transfer count of 00 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.