Rewiring the Lab
A university computer lab has n computers, numbered from 1 to n, and m network cables. Each
cable directly links two different computers. Over the years the cabling has become a mess: some
pairs are linked by several cables, some cables close loops, and some computers have been cut off
from the rest.
Two computers can talk to each other if one can be reached from the other by following cables, possibly passing through other computers. The lab technician wants every computer to be able to talk to every other one. No new cables can be bought, but in one move the technician can unplug any single cable from both of its ends and plug it back in between any two different computers.
Find the minimum number of moves needed, or report that it can't be done with the cables the lab has.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^5, 0 ≤ m ≤ 2·10^5): the number of
computers and the number of cables.
Each of the next m lines contains two integers a_i and b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i): the
computers joined by the i-th cable. Several cables may join the same pair of computers.
Output
Print one integer: the minimum number of moves after which every computer can reach every other
one, or -1 if that's impossible no matter how the cables are rearranged.
Notes
In the first example, computers 1, 2 and 3 already reach each other, computers 4 and 5 form a second group, and computer 6 is on its own. The cables 3–1 and 2–1 are not needed to keep the first group together, so one of them can be re-plugged between 3 and 4 and the other between 5 and 6: two moves.
In the second example the lab has only 3 cables, but linking 5 computers together needs at least 4.