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: current←{start_vertex} 2: visited←current 3: while visited≠ the set of all the vertices 4: found←{} 5: for v in current 6: for each u adjacent to v 7: found←found∪{u} 8: current←found∖visited 9: visited←visited∪found
However, Mr. Endo apparently forgot to manage visited vertices in his code. More precisely, he wrote the following code:
1: current←{start_vertex} 2: while current≠ the set of all the vertices 3: found←{} 4: for v in current 5: for each u adjacent to v 6: found←found∪{u} 7: current←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.
The input consists of a single test case formatted as follows.
N M U1 V1 ... UM VM
The first line consists of two integers N (2≤N≤100,000) and M (1≤M≤100,000), where N is the number of vertices and M is the number of edges in a given undirected graph, respectively. The i-th line of the following M lines consists of two integers Ui and Vi (1≤Ui,Vi≤N), which means the vertices Ui and Vi are adjacent in the given graph. The vertex 1 is the start vertex, i.e. start_vertex in the pseudo codes. You can assume that the given graph also meets the following conditions.
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.
3 3 1 2 1 3 2 3
2
4 3 1 2 2 3 3 4
-1
Transition of current is {1}→{2}→{1,3}→{2,4}→{1,3}→{2,4}→.... Although Mr. Endo's program will achieve to visit all the vertices (in 3 steps), will never become the same set as all the vertices.
4 4 1 2 2 3 3 4 4 1
-1
8 9 2 1 3 5 1 6 2 5 3 1 8 4 2 7 7 1 7 4
3