← 返回 uber 的题目列表Bus Routes
类型:online_judge
Problem: Bus Routes
You are given several bus routes routes, where routes[i] is the list of stops visited cyclically by the i-th bus.
You start at stop source and want to reach stop target. Taking one bus counts as one ride. You may travel between any stops on the same route without extra cost, and you may transfer to another bus at any stop shared by multiple routes.
Return the minimum number of buses you need to take to reach target. If it is impossible, return -1.
Input Format
First line: integer n, the number of bus routes.
Next n lines: integers representing stops on one route.
Last line: two integers source target.
Output Format
Print one integer: the minimum number of buses required.
Constraints
1 <= n <= 500
1 <= routes[i].length <= 10^5
Total number of stops across all routes is at most 10^5
0 <= routes[i][j], source, target <= 10^6
Example
Input:
2
1 2 7
3 6 7
1 6
Output:
2
Example
Input
2
1 2 7
3 6 7
1 6
Output
2