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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int Maxn = 100010;
struct node {
	int y, next;
}a[Maxn*2]; int first[Maxn], len;
void ins(int x, int y) {
	len++;
	a[len].y = y;
	a[len].next = first[x]; first[x] = len;
}
int n, m, bsum[Maxn], bs[Maxn], size[Maxn], ans;
int dfn[Maxn], id, co[Maxn], fzx, fzy;
int p[Maxn], pl;
bool state;
int _abs(int x) { return x < 0 ? -x : x; }
void dfs(int x, int fk) {
	dfn[x] = ++id; bsum[x] = co[x] == 1 ? 1 : 0; size[x] = 1;
	for(int k = first[x]; k; k = a[k].next){
		int y = a[k].y;
		if((k+1)/2 == (fk+1)/2) continue;
		if(dfn[y] == 0){
			co[y] = co[x]^1;
			dfs(y, k);
			if(state == false && dfn[y] <= dfn[fzx] && dfn[fzx] <= dfn[y]+size[y]-1 && (dfn[y] > dfn[fzy] || dfn[fzy] > dfn[y]+size[y]-1)) p[++pl] = 2*bsum[y]-size[y];
			else ans += _abs(size[y]-2*bsum[y]);
			bsum[x] += bsum[y];
			size[x] += size[y];
		} else {
			fzx = x; fzy = y;
			if(co[y] != co[x]) state = false;
		}
	}
}
void dfs2(int x, int fk) {
	dfn[x] = ++id; bsum[x] = co[x] == 1 ? 1 : 0; size[x] = 1;
	bs[x] = bsum[x];
	if(x == fzx || x == fzy) bs[x] += (n-2*bsum[0])/2;
	for(int k = first[x]; k; k = a[k].next){
		int y = a[k].y;
		if((k+1)/2 == (fk+1)/2) continue;
		if(dfn[y] == 0){
			co[y] = co[x]^1;
			dfs2(y, k);
			ans += _abs(size[y]-bsum[y]-bs[y]);
			bsum[x] += bsum[y];
			bs[x] += bs[y];
			size[x] += size[y];
		}
	}
}
void case1() {
	ans = 0;
	memset(dfn, 0, sizeof(dfn)); id = 0;
	state = true; dfs(1, 0);
	if(bsum[1]*2 != n){ printf("-1\n"); return; }
	printf("%d\n", ans);
}
void case2() {
	if(bsum[1]%2 != (n-bsum[1])%2){ printf("-1\n"); return; }
	bsum[0] = bsum[1]; ans = 0;
	memset(dfn, 0, sizeof(dfn)); id = 0;
	dfs2(1, 0);
	printf("%d\n", ans+_abs(n-2*bsum[0])/2);
}
void case3() {
	if(bsum[1]*2 != n){ printf("-1\n"); return; }
	sort(p+1, p+pl+1);
	int o;
	for(int i = 1; i <= pl; i++) p[i] <<= 1;
	if(pl%2 == 0) o = (p[pl>>1]+p[(pl>>1)+1])>>1;
	else o = p[(pl+1)>>1];
	for(int i = 1; i <= pl; i++) ans += _abs(p[i]-o)>>1;
	ans += _abs(o)>>1;
	printf("%d\n", ans);
}
int main() {
	int i, j, k, T;
	T=1;
	while(T--){
		scanf("%d%d", &n, &m);
		len = 0; memset(first, 0, sizeof(first));
		for(i = 1; i <= m; i++){
			int x, y;
			scanf("%d%d", &x, &y);
			ins(x, y); ins(y, x);
		}
		if(n == m+1) case1();
		else {
			memset(dfn, 0, sizeof(dfn)); id = 0;
			state = true; pl = 0; ans = 0; dfs(1, 0);
			if(state == 1) case2();
			else case3();
		}
	}
	return 0;
}