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<cstdio>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=100000+10;
int h[maxn],go[maxn*2],nxt[maxn*2],fa[maxn],co[maxn],kk[maxn],bb[maxn],sta[maxn];
int i,j,k,l,t,n,m,u,v,a,b,x,tot,top,num;
ll ans;
bool czy,gjx;
void add(int x,int y){
	go[++tot]=y;
	nxt[tot]=h[x];
	h[x]=tot;
}
int getfa(int x){
	if (!fa[x]) return x;
	int t=getfa(fa[x]);
	co[x]=co[x]^co[fa[x]];
	return fa[x]=t;
}
void dfs(int x,int y){
	int t=h[x];
	while (t){
		if (go[t]!=y){
			dfs(go[t],x);
			kk[x]+=kk[go[t]];
			bb[x]+=bb[go[t]];
		}
		t=nxt[t];
	}
}
int main(){
	scanf("%d%d",&n,&m);
	fo(i,1,m){
		scanf("%d%d",&j,&k);
		a=getfa(j);b=getfa(k);
		if (a!=b){
			add(j,k);add(k,j);
			fa[a]=b;
			co[a]=co[j]^co[k]^1;
		}
		else{
			u=j;v=k;
			if (co[j]==co[k]) gjx=1;
		}
	}
	fo(i,1,n) getfa(i);
	if (m==n-1){
		fo(i,1,n)
			if (co[i]) num++;else num--;
		if (num){
			printf("-1\n");
			return 0;
		}
		fo(i,1,n){
			kk[i]=0;
			if (co[i]) bb[i]=1;else bb[i]=-1;
		}
	}
	else{
		if (gjx){
			/*if (co[u])
				fo(i,1,n) co[i]^=1;*/
			fo(i,1,n)
				if (co[i]) num++;else num--;
			if (num%2==1){
				printf("-1\n");
				return 0;
			}
			fo(i,1,n){
				kk[i]=0;
				if (co[i]) bb[i]=1;else bb[i]=-1;
			}
			bb[u]-=num/2;bb[v]-=num/2;
			ans+=(ll)abs(num/2);
			//ans+=num/2;
		}
		else{
			fo(i,1,n)
				if (co[i]) num++;else num--;
			if (num){
				printf("-1\n");
				return 0;
			}
			fo(i,1,n){
				kk[i]=0;
				if (co[i]) bb[i]=1;else bb[i]=-1;
			}
			kk[u]=1;kk[v]=-1;
		}
	}
	dfs(1,0);
	fo(i,1,n)
		if (!kk[i]) ans+=(ll)abs(bb[i]);
		else{
			if (kk[i]) bb[i]=-bb[i];
			sta[++top]=bb[i];
		}
	sta[++top]=0;
	sort(sta+1,sta+top+1);
	x=sta[(top+1)/2];
	fo(i,1,top) ans+=(ll)abs(sta[i]-x);
	printf("%lld\n",ans);
}