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<bits/stdc++.h>
using namespace std;
#define N 1111116
#define next nxt

int n,m,tx,ty,x[N],y[N],next[N],p[N],f[N],c[N],s[N],k[N];
vector<int> q;

void travel(int x){
	for (int t=p[x];t;t=next[t])
		if (y[t]!=f[x]){
			if (!c[y[t]])
			f[y[t]]=x,c[y[t]]=-c[x],travel(y[t]);
			else tx=x,ty=y[t];
		}
}

void solve(int x){
	s[x]=c[x];
	for (int t=p[x];t;t=next[t])
		if (y[t]!=f[x]&&!(x==tx&&y[t]==ty)&&!(x==ty&&y[t]==tx))
		solve(y[t]),s[x]+=s[y[t]],k[x]+=k[y[t]];
}

int read(){
	int x=0,f=1;char ch=getchar();
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-f;
	for (;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
	return x*f;
}

int main(){
	n=read();m=read();
	for (int i=1;i<=m;i++){
		x[2*i-1]=y[2*i]=read();
		y[2*i-1]=x[2*i]=read();
		next[2*i-1]=p[x[2*i-1]];p[x[2*i-1]]=2*i-1;
		next[2*i]=p[x[2*i]];p[x[2*i]]=2*i;
	}
	c[1]=1;travel(1);long long ans=0;
	if (m==n-1){
		solve(1);
		if (s[1]) {printf("-1\n");return 0;};
		for (int i=1;i<=n;i++) ans+=abs(s[i]);
	}
	else if (c[tx]==c[ty]){
		solve(1);
		if (s[1]%2) {printf("-1\n");return 0;};
		c[tx]-=s[1]/2;c[ty]-=s[1]/2;
		ans=abs(s[1]/2);solve(1);
		for (int i=1;i<=n;i++) ans+=abs(s[i]);
	}
	else {
		k[tx]=1;k[ty]=-1;solve(1);
		if (s[1]) {printf("-1\n");return 0;};
		for (int i=1;i<=n;i++)
			if (k[i]==0) ans+=abs(s[i]);
			else q.push_back(s[i]*k[i]);
		q.push_back(0);
		sort(q.begin(),q.end());
		for (int i=0,j=(int)q.size()-1;i<j;i++,j--)
			ans+=q[j]-q[i];
	}
	printf("%lld\n",ans);
	return 0;
}