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>
const int N = 1e5 + 54;
int n, m;
namespace Tree {
	struct node { int u, v; } edge[N << 1];
	int head[N], v[N], col[N], cnt;
	long long sum, ans;
	inline void add(int x, int y) { edge[cnt].u = head[x], edge[cnt].v = y, head[x] = cnt++; }
	void dfs(int x, int fa) {
		int i;
		for (i = head[x]; ~i; i = edge[i].u) {
			int y = edge[i].v;
			if (y == fa) continue;
			col[y] = 1 - col[x], dfs(y, x);
		}
	}
	void solve(int x, int fa) {
		int i;
		for (i = head[x]; ~i; i = edge[i].u) {
			int y = edge[i].v;
			if (y == fa) continue;
			solve(y, x);
			v[x] += v[y];
		}
	}
	void SOLVE() {
		int x, y, i;
		memset(head, -1, sizeof head);
		for (i = 1; i <= m; ++i) scanf("%d%d", &x, &y), add(x, y), add(y, x);
		col[1] = 1, dfs(1, 0);
		for (i = 1; i <= n; ++i) {
			if (col[i]) v[i] = 1;
			else v[i] = -1;
			sum += v[i];
		}
		if (sum) {
			puts("-1");
			return;
		}
		solve(1, 0);
		for (i = 1; i <= n; ++i) ans += abs(v[i]);
		printf("%lld\n", ans);
	}
}

namespace BRT {
	struct Edge { int id, x, y; } E;
	struct node { int u, v; } edge[N << 1];
	int head[N], v[N], col[N], cnt, tot, p[N], vis[N], f[N];
	long long sum, ans;
	inline void add(int x, int y) { edge[cnt].u = head[x], edge[cnt].v = y, head[x] = cnt++; }
	void dfs(int x, int fa) {
		int i;
		vis[x] = 1;
		for (i = head[x]; ~i; i = edge[i].u) {
			int y = edge[i].v;
			if (y == fa) continue;
			if (vis[y]) E.id = i, E.x = x, E.y = y;
			else col[y] = 1 - col[x], dfs(y, x);
		}
	}
	void solve(int x, int fa) {
		int i;
		for (i = head[x]; ~i; i = edge[i].u) {
			int y = edge[i].v;
			if (y == fa || i == E.id || (i ^ 1) == E.id) continue;
			solve(y, x), v[x] += v[y], f[x] += f[y];
		}
	}
	void SOLVE() {
		memset(head, -1, sizeof head);
		int x, y, i;
		for (i = 1; i <= m; ++i) scanf("%d%d", &x, &y), add(x, y), add(y, x);
		col[1] = 1, dfs(1, 0);
		for (i = 1; i <= n; ++i) {
			if (col[i]) v[i] = 1;
			else v[i] = -1;
			sum += v[i];
		}
		if (col[E.x] == col[E.y]) {
			if (abs(sum) & 1) {
				puts("-1");
				return;
			}
			v[E.x] -= sum / 2, v[E.y] -= sum / 2, ans += abs(sum / 2);
		} else {
			if (sum) {
				puts("-1");
				return;
			}
			f[E.x] = 1, f[E.y] = -1;
		}
		solve(1, 0);
		if (col[E.x] == col[E.y]) {
			for (i = 1; i <= n; ++i) ans += abs(v[i]);
			printf("%lld\n", ans);
			return;
		}
		for (i = 1; i <= n; ++i)
			if (f[i] == 0) ans += abs(v[i]);
			else p[++tot] = f[i] * v[i];
		p[++tot] = 0;
		std::sort(p + 1, p + tot + 1);
		int mid = p[(tot + 1) >> 1];
		for (i = 1; i <= tot; ++i) ans += abs(p[i] - mid);
		printf("%lld\n", ans);
	}
}
int main() {
	freopen("b.in", "r", stdin); freopen("b.out", "w", stdout);
	scanf("%d%d", &n, &m);
	if (m == n - 1) Tree::SOLVE();
	else BRT::SOLVE();
}