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: Runtime Error)

#include "bits/stdc++.h"
using namespace std;
vector <int> g[100010];
long long opt[100010];
long long ans;
bool cyc[100010];
bool vis[100010];
int from[100010];
vector <int> can;

void find_cycle(int x, int par) {
	vis[x] = true;
	from[x] = par;
	for(auto i : g[x]) {
		if(!vis[i]) {
			find_cycle(i, x);
		} else if (i != par && can.empty()) {
			int cur = x;
			while(cur != i) {
				cyc[cur] = true;
				can.push_back(cur);
				cur = from[cur];
			}
			can.push_back(cur);
			cyc[cur] = true;
		}
	}
}

void dfs(int x, int par) {
	opt[x] = 1;
	for(auto i : g[x]) {
		if(i == par || cyc[i]) continue;
		dfs(i, x);
		opt[x] -= opt[i];
	}
	ans += abs(opt[x]);
}

int main(int argc, char const *argv[])
{
	int n, m;
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= m; i++) {
		int p, q;
		scanf("%d %d", &p, &q);
		g[p].push_back(q);
		g[q].push_back(p);
	}
	if(n % 2 == 1) {
		printf("-1\n");
		exit(0);
	}
	if(m == n-1) {
		dfs(1, 0);
		if(opt[1] == 0) {
			printf("%lld\n", ans);
		} else printf("-1\n");
		exit(0);
	}
	find_cycle(1, 0);

	int sz = can.size();
	vector <long long> cost (sz); 
	vector <long long> p (sz);

	for(int i = 0; i < sz; i++) {
		dfs(can[i], 0);
		cost[i] = opt[can[i]];
		ans -= abs(cost[i]);
		// cout << can[i] << ' ' << cost[i] << endl;
		if(i > 0) {
			if(i & 1) p[i] = p[i - 1] - cost[i];
			else p[i] = p[i - 1] + cost[i]; 
		}
	}
	long long start;
	if(sz & 1) {
		long long sum = p[sz - 1] - cost[0];
		if(sum & 1) {
			printf("-1\n");
			exit(0);
		}
		start = sum / 2;
		assert(false);
	} else {
		sort(p.begin(), p.end());
		start = p[sz / 2];
	}
	for(int i = 0; i < sz; i++) {
		ans += abs(start - p[i]);
	}
	printf("%lld\n", ans);
	return 0;
}