This page is still under construction.

Parts of this page are still being built. What you see may change.

Transfer

Time limit2sMemory limit256 MB

Summary
Given hypertubes each connecting K stations totally, find the fewest stations visited traveling from station 1 to station N.
Level

Medium5 of 10

Topics
BFS, Graph, Hash map, Implementation
Solved
No attempts yet

Problem

In the far future, the most widely used form of public transportation is the hypertube. A single hypertube directly connects KK stations to one another; that is, you can travel between any two stations on the same hypertube in a single move. Starting from station 11 and arriving at station NN, find the minimum number of stations you visit. (Both the starting station and the destination station count as visited.)

Input

The first line contains the number of stations NN, the number of stations that one hypertube connects KK, and the number of hypertubes MM. (1≤N≤1000001 \le N \le 100000, 1≤K,M≤10001 \le K, M \le 1000)

Each of the next MM lines describes one hypertube. A line contains KK integers: the numbers of the stations that this hypertube connects to one another.

Output

Print the minimum number of stations visited on a trip from station 11 to station NN. If station NN cannot be reached, print −1-1.

Examples2

  1. Example 1

    Input
    9 3 5
    1 2 3
    1 4 5
    3 6 7
    5 6 7
    6 8 9
    
    Expected output
    4
    
  2. Example 2

    Input
    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
    
    Expected output
    3