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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

vector <int> from[100010], circle;
int size[100010], vis[100010], fa[100010], val[100010];
int s, e;

void dfs(int x, int last, int p){
	fa[x] = last;
	if (p != 0) val[x] = size[x] = p;
	else size[x] = val[x];
	for (int i = 0; i < from[x].size(); i++){
		int v = from[x][i];
		if (v == last) continue;
		if (x == s && v == e || v == s && x == e) continue;
		dfs(v, x, -p), size[x] += size[v];
	}
}

void find_circle(int x, int last){
	vis[x] = 1, fa[x] = last;
	for (int i = 0; i < from[x].size(); i++){
		int v = from[x][i];
		if (v == last) continue;
		if (x == s && v == e || v == s && x == e) continue;
		if (vis[v]){
			int now = x;
			while (now != v){
				circle.push_back(now);
				now = fa[now];
			}
			circle.push_back(v);
			return ;
		}
		find_circle(v, x);
		if (circle.size()) return ;
	}
}

int main(){
	int n, m; scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++){
		int u, v; scanf("%d%d", &u, &v);
		from[u].push_back(v);
		from[v].push_back(u);
	}
	if (m == n - 1){
		dfs(1, 1, 1);
		if (size[1] != 0){
			printf("-1\n");
			return 0;
		}
		long long ans = 0;
		for (int i = 1; i <= n; i++){
			ans += abs(size[i]);
		}
		printf("%lld\n", ans);
		return 0;
	}
	find_circle(1, 1);
	if (circle.size() & 1){
		s = circle[0], e = circle[1];
		dfs(s, s, 1);
		if (size[s] & 1){
			printf("-1\n");
			return 0;
		}
		val[s] -= size[s] / 2;
		long long ans = abs(size[s] / 2);
		dfs(s, s, 0);
		for (int i = 1; i <= n; i++){
			ans += abs(size[i]);
		}
		printf("%lld\n", ans);
	} else {
//		for (int test = 0; test < circle.size(); test++){
			s = circle[0], e = circle[1];
			dfs(s, s, 1);
			if (size[s]){
				printf("-1\n");
				return 0;
			}
			vector <int> all;
			int now = e;
			while (now != s){
				all.push_back(size[now]);
				vis[now] = 1, now = fa[now];
			}
			all.push_back(0);
			sort(all.begin(), all.end());
			int pos = all[all.size() / 2];
			long long ans = abs(pos);
			for (int i = 1; i <= n; i++){
				if (i == s) continue;
				if (vis[i]) ans += abs(size[i] - pos);
				else ans += abs(size[i]);
			}
			printf("%lld\n", ans);
//		}
		return 0;
	}
	return 0;
}