Problem:
Score : 2200 points

Problem Statement

You are given an undirected graph with N vertices and M edges.
Here, N-1≤M≤N holds and the graph is connected.
There are no self-loops or multiple edges in this graph.

The vertices are numbered 1 through N, and the edges are numbered 1 through M.
Edge i connects vertices a_i and b_i.

The color of each vertex can be either white or black.
Initially, all the vertices are white.
Snuke is trying to turn all the vertices black by performing the following operation some number of times:

Select a pair of adjacent vertices with the same color, and invert the colors of those vertices. That is, if the vertices are both white, then turn them black, and vice versa.

Determine if it is possible to turn all the vertices black. If the answer is positive, find the minimum number of times the operation needs to be performed in order to achieve the objective.

Constraints

2≤N≤10^5

N-1≤M≤N

1≤a_i,b_i≤N

There are no self-loops or multiple edges in the given graph.

The given graph is connected.

Partial Score

In the test set worth 1500 points, M=N-1.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

If it is possible to turn all the vertices black, print the minimum number of times the operation needs to be performed in order to achieve the objective.
Otherwise, print -1 instead.

Sample Input 1

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

Sample Output 1

5

For example, it is possible to perform the operations as shown in the following diagram:

Sample Input 2

3 2
1 2
2 3

Sample Output 2

-1

It is not possible to turn all the vertices black.

Sample Input 3

4 4
1 2
2 3
3 4
4 1

Sample Output 3

2

This case is not included in the test set for the partial score.

Sample Input 4

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

Sample Output 4

7

This case is not included in the test set for the partial score.

Solution in Pascal (FPC 2.6.2) (judge verdict: Accepted)

var
  a, f, h, tmp: array [0..100000] of longint;
  u, v: array [1..200000] of longint;
  n, m, i, x, y, s, rt, re, cnt: longint;
procedure swap(var x, y: longint); inline;
var
  t: longint;
begin
  t := x;
  x := y;
  y := t;
end;
function find(x: longint): longint;
begin
  if x <> f[x] then f[x] := find(f[x]);
  exit(f[x]);
end;
procedure add(x, y: longint); inline;
begin
  inc(cnt);
  u[cnt] := h[x];
  v[cnt] := y;
  h[x] := cnt;
end;
procedure dfs(x, s: longint);
var
  i: longint;
begin
  a[x] := s;
  i := h[x];
  while i > 0 do
  begin
    if v[i] <> f[x] then
    begin
      f[v[i]] := x;
      dfs(v[i], -s);
      inc(a[x], a[v[i]]);
    end;
    i := u[i];
  end;
end;
procedure quicksort(l, r: longint);
var
  i, j, m: longint;
begin
  i := l;
  j := r;
  m := tmp[(l + r) shr 1];
  repeat
    while tmp[i] < m do inc(i);
    while tmp[j] > m do dec(j);
    if i <= j then
    begin
      swap(tmp[i], tmp[j]);
      inc(i);
      dec(j);
    end;
  until i > j;
  if l < j then quicksort(l, j);
  if i < r then quicksort(i, r);
end;
begin
  read(n, m);
  for i := 1 to n do f[i] := i;
  cnt := 0;
  rt := 1;
  re := 1;
  for i := 1 to m do
  begin
    read(x, y);
    if find(x) <> find(y) then
    begin
      f[find(x)] := find(y);
      add(x, y);
      add(y, x);
    end
    else
    begin
      rt := x;
      re := y;
    end;
  end;
  f[rt] := 0;
  dfs(rt, 1);
  if n = m then
  begin
    cnt := 0;
    i := re;
    while i > 0 do
    begin
      inc(cnt);
      tmp[cnt] := a[i];
      i := f[i];
    end;
    if odd(cnt) then
    begin
      if odd(a[rt]) then
      begin
        writeln(-1);
        halt;
      end;
      i := re;
      while i > 0 do
      begin
        dec(a[i], a[rt] div 2);
        i := f[i];
      end;
    end
    else
    begin
      if a[rt] <> 0 then
      begin
        writeln('-1');
        halt;
      end;
      quicksort(1, cnt);
      i := re;
      while i > 0 do
      begin
        dec(a[i], tmp[cnt shr 1]);
        i := f[i];
      end;
    end;
  end
  else
    if a[rt] <> 0 then
    begin
      writeln(-1);
      halt;
    end;
  s := 0;
  for i := 1 to n do inc(s, abs(a[i]));
  writeln(s);
end.