#T123. 最优乘车 (travel)

最优乘车 (travel)

Description

H City is a popular tourist destination, attracting thousands of visitors every year. To facilitate tourists, the bus company has set up bus stops at various attractions, hotels, restaurants, and other locations, and operates several one-way bus routes. Each one-way bus route starts at a specific bus stop, passes through several stops in sequence, and finally arrives at the terminal stop.

A traveler recently visited H City and wants to visit S Park. However, if there is no direct bus route from his hotel to S Park, he may need to take one bus for a few stops, then transfer to another bus at the same stop, repeating this process until he reaches S Park.

All bus stops in H City are numbered with integers 1, 2, ..., N. The bus stop at the traveler's hotel is numbered 1, and the bus stop at S Park is numbered N.

Write a program to help the traveler find an optimal travel plan that minimizes the number of transfers required to get from the hotel to S Park.

Input Format

The first line contains two numbers, M and N (1 ≤ M ≤ 100, 1 < N ≤ 500), indicating that there are M one-way bus routes and a total of N bus stops. From the second line to the (M+1)-th line, the information for each bus route is provided sequentially. The (i+1)-th line describes the i-th bus route, listing all the stops along the route in order, with adjacent stop numbers separated by a space.

Output Format

Only one line of output is required. If it is impossible to reach S Park from the hotel by bus, output "NO". Otherwise, output the minimum number of transfers determined by your program. A transfer count of 0 means no transfers are needed to reach the destination.

3 7
6 7
4 7 3 6
2 1 3 5

2