시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 256 MB68851696116125.904%

문제

아주 먼 미래에 사람들이 가장 많이 사용하는 대중교통은 하이퍼튜브이다. 하이퍼튜브 하나는 역 K개를 서로 연결한다. 1번역에서 N번역으로 가는데 방문하는 최소 역의 수는 몇 개일까?

입력

첫째 줄에 역의 수 N과 한 하이퍼튜브가 서로 연결하는 역의 개수 K, 하이퍼튜브의 개수 M이 주어진다. (1 ≤ N ≤ 100,000, 1 ≤ K, M ≤ 1000)

다음 M개 줄에는 하이퍼튜브의 정보가 한 줄에 하나씩 주어진다. 총 K개 숫자가 주어지며, 이 숫자는 그 하이퍼튜브가 서로 연결하는 역의 번호이다. 

출력

첫째 줄에 1번역에서 N번역으로 가는데 방문하는 역의 개수의 최솟값을 출력한다. 만약, 갈 수 없다면 -1을 출력한다.

예제 입력 1

9 3 5
1 2 3
1 4 5
3 6 7
5 6 7
6 8 9

예제 출력 1

4

1-3-6-9나 1-5-6-9로 이동하면 된다.

예제 입력 2

15 8 4
11 12 8 14 13 6 10 7
1 5 8 12 13 6 2 4
10 15 4 5 9 8 14 12
11 12 14 3 5 6 1 13

예제 출력 2

3
W3sicHJvYmxlbV9pZCI6IjUyMTQiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWQ2NThcdWMyYjkiLCJkZXNjcmlwdGlvbiI6IjxwPlx1YzU0NFx1YzhmYyBcdWJhM2MgXHViYmY4XHViNzk4XHVjNWQwIFx1YzBhY1x1Yjc4Y1x1YjRlNFx1Yzc3NCBcdWFjMDBcdWM3YTUgXHViOWNlXHVjNzc0IFx1YzBhY1x1YzZhOVx1ZDU1OFx1YjI5NCBcdWIzMDBcdWM5MTFcdWFkNTBcdWQxYjVcdWM3NDAgXHVkNTU4XHVjNzc0XHVkMzdjXHVkMjljXHViZTBjXHVjNzc0XHViMmU0LiBcdWQ1NThcdWM3NzRcdWQzN2NcdWQyOWNcdWJlMGMgXHVkNTU4XHViMDk4XHViMjk0IF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