#D2769. Revenge of the Endless BFS

    ID: 2309 Type: Default 2000ms 536MiB

Revenge of the Endless BFS

Revenge of the Endless BFS

Mr. Endo wanted to write the code that performs breadth-first search (BFS), which is a search algorithm to explore all vertices on a directed graph. An example of pseudo code of BFS is as follows:

1: current{start_vertex}current \leftarrow \{start\_vertex\} 2: visitedcurrentvisited \leftarrow current 3: while visitedvisited \ne the set of all the vertices 4: found{}found \leftarrow \{\} 5: for uu in currentcurrent 6: for each vv such that there is an edge from uu to vv 7: foundfound{v}found \leftarrow found \cup \{v\} 8: currentfoundvisitedcurrent \leftarrow found \setminus visited 9: visitedvisitedfoundvisited \leftarrow visited \cup found

However, Mr. Endo apparently forgot to manage visited vertices in his code. More precisely, he wrote the following code:

1: current{start_vertex}current \leftarrow \{start\_vertex\} 2: while currentcurrent \ne the set of all the vertices 3: found{}found \leftarrow \{\} 4: for uu in currentcurrent 5: for each vv such that there is an edge from uu to vv 6: foundfound{v}found \leftarrow found \cup \{v\} 7: currentfoundcurrent \leftarrow found

You may notice that for some graphs, Mr. Endo's program will not stop because it keeps running infinitely. Notice that it does not necessarily mean the program cannot explore all the vertices within finite steps. Your task here is to make a program that determines whether Mr. Endo's program will stop within finite steps for a given directed graph in order to point out the bug to him. Also, calculate the minimum number of loop iterations required for the program to stop if it is finite. Since the answer might be huge, thus print the answer modulo 109+710^9 +7, which is a prime number.

Input

The input consists of a single test case formatted as follows.

NN MM u1u_1 v1v_1 : uMu_M vMv_M

The first line consists of two integers NN (2N5002 \leq N \leq 500) and MM (1M200,0001 \leq M \leq 200,000), where NN is the number of vertices and MM is the number of edges in a given directed graph, respectively. The ii-th line of the following MM lines consists of two integers uiu_i and viv_i (1ui,viN1 \leq u_i, v_i \leq N), which means there is an edge from uiu_i to viv_i in the given graph. The vertex 11 is the start vertex, i.e. startvertexstart\\_vertex in the pseudo codes. You can assume that the given graph also meets the following conditions.

  • The graph has no self-loop, i.e., uiviu_i \ne v_i for all 1iM1 \leq i \leq M.
  • The graph has no multi-edge, i.e., (ui,vi)(uj,vj)(u_i, v_i) \le (u_j, v_j) for all 1i<jM1 \leq i < j \leq M.
  • For each vertex vv, there is at least one path from the start vertex 11 to vv.

Output

If Mr. Endo's wrong BFS code cannot stop within finite steps for the given input directed graph, print '-1' in a line. Otherwise, print the minimum number of loop iterations required to stop modulo 109+710^9+7.

Examples

Input

4 4 1 2 2 3 3 4 4 1

Output

-1

Input

4 5 1 2 2 3 3 4 4 1 1 3

Output

7

Input

5 13 4 2 2 4 1 2 5 4 5 1 2 1 5 3 4 3 1 5 4 5 2 3 5 2 1 3

Output

3

inputFormat

Input

The input consists of a single test case formatted as follows.

NN MM u1u_1 v1v_1 : uMu_M vMv_M

The first line consists of two integers NN (2N5002 \leq N \leq 500) and MM (1M200,0001 \leq M \leq 200,000), where NN is the number of vertices and MM is the number of edges in a given directed graph, respectively. The ii-th line of the following MM lines consists of two integers uiu_i and viv_i (1ui,viN1 \leq u_i, v_i \leq N), which means there is an edge from uiu_i to viv_i in the given graph. The vertex 11 is the start vertex, i.e. startvertexstart\\_vertex in the pseudo codes. You can assume that the given graph also meets the following conditions.

  • The graph has no self-loop, i.e., uiviu_i \ne v_i for all 1iM1 \leq i \leq M.
  • The graph has no multi-edge, i.e., (ui,vi)(uj,vj)(u_i, v_i) \le (u_j, v_j) for all 1i<jM1 \leq i < j \leq M.
  • For each vertex vv, there is at least one path from the start vertex 11 to vv.

outputFormat

Output

If Mr. Endo's wrong BFS code cannot stop within finite steps for the given input directed graph, print '-1' in a line. Otherwise, print the minimum number of loop iterations required to stop modulo 109+710^9+7.

Examples

Input

4 4 1 2 2 3 3 4 4 1

Output

-1

Input

4 5 1 2 2 3 3 4 4 1 1 3

Output

7

Input

5 13 4 2 2 4 1 2 5 4 5 1 2 1 5 3 4 3 1 5 4 5 2 3 5 2 1 3

Output

3

样例

4 4
1 2
2 3
3 4
4 1
-1