#jag2017summerday3f. [jag2017summer_day3_f]Endless BFS

[jag2017summer_day3_f]Endless BFS

MathJax.Hub.Config({ tex2jax: { inlineMath: [["","",""], ["\\(","\\)"]], processEscapes: true }}); blockquote { font-family: Menlo, Monaco, "Courier New", monospace; color: #333333; display: block; padding: 8.5px; margin: 0 0 9px; font-size: 16px; line-height: 18px; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; white-space: pre; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; }

Problem Statement

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

1: currentleftarrowstart_vertexcurrent \\leftarrow \\{start\\\_vertex\\} 2: visitedleftarrowcurrentvisited \\leftarrow current 3: while visitednevisited \\ne the set of all the vertices 4: foundleftarrowfound \\leftarrow \\{\\} 5: for vv in currentcurrent 6: for each uu adjacent to vv 7: foundleftarrowfoundcupufound \\leftarrow found \\cup \\{u\\} 8: currentleftarrowfoundsetminusvisitedcurrent \\leftarrow found \\setminus visited 9: visitedleftarrowvisitedcupfoundvisited \\leftarrow visited \\cup found

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

1: currentleftarrowstart_vertexcurrent \\leftarrow \\{start\\\_vertex\\} 2: while currentnecurrent \\ne the set of all the vertices 3: foundleftarrowfound \\leftarrow \\{\\} 4: for vv in currentcurrent 5: for each uu adjacent to vv 6: foundleftarrowfoundcupufound \\leftarrow found \\cup \\{u\\} 7: currentleftarrowfoundcurrent \\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. See example 2 below for more details.Your task here is to make a program that determines whether Mr. Endo's program will stop within finite steps for a given 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.


Input

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

NN MM U_1U\_{1} V_1V\_{1} vdots\\vdots U_MU\_{M} V_MV\_{M}

The first line consists of two integers NN (2leNle100,0002 \\le N \\le 100{,}000) and MM (1leMle100,0001 \\le M \\le 100{,}000), where NN is the number of vertices and MM is the number of edges in a given undirected graph, respectively. The ii-th line of the following MM lines consists of two integers U_iU\_{i} and V_iV\_{i} (1leU_i,V_ileN1 \\le U\_{i}, V\_{i} \\le N), which means the vertices U_iU\_{i} and V_iV\_{i} are adjacent in the given graph. The vertex 1 is the start vertex, i.e. start_vertexstart\\\_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., U_ineV_iU\_{i} \\ne V\_{i} for all 1leileM1 \\le i \\le M.
  • The graph has no multi-edge, i.e., U_i,V_ineU_j,V_j\\{U\_{i}, V\_{i}\\} \\ne \\{U\_{j}, V\_{j}\\} for all 1lei<jleM1 \\le i < j \\le M.
  • The graph is connected, i.e., there is at least one path from UU to VV (and vice versa) for all vertices 1leU,VleN1 \\le U, V \\le N.

Output

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


Sample Input 1

3 3
1 2
1 3
2 3

Output for Sample Input 1

2

Sample Input 2

4 3
1 2
2 3
3 4

Output for Sample Input 2

-1

Transition of currentcurrent is $\\{1\\} \\rightarrow \\{2\\} \\rightarrow \\{1, 3\\} \\rightarrow \\{2, 4\\} \\rightarrow \\{1, 3\\} \\rightarrow \\{2, 4\\} \\rightarrow \\cdots$. Although Mr. Endo's program will achieve to visit all the vertices (in 3 steps), currentcurrent will never become the same set as all the vertices.


Sample Input 3

4 4
1 2
2 3
3 4
4 1

Output for Sample Input 3

-1

Sample Input 4

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

Output for Sample Input 4

3