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> 
#define N 100005
using namespace std;
struct edge{
	int t,next;
}e[N*2];int ecnt,head[N];
void addedge(int f,int t){
	e[++ecnt]=(edge){t,head[f]};head[f]=ecnt;
	e[++ecnt]=(edge){f,head[t]};head[t]=ecnt;
}
long long ans;
int ini[N],ax[N],delta[N],del;
void DFS(int u,int fr){
	delta[u]=ini[u];
	for(int i=head[u];i;i=e[i].next){
		if(e[i].t!=fr&&i!=del&&i!=(del^1)){
			DFS(e[i].t,u);
			delta[u]-=delta[e[i].t];
			ax[u]-=ax[e[i].t];
		}
	}
	ax[u]==0? ans+=abs(delta[u]):0;
}
int vis[N],num[2],A,B,type[N];
void DFS2(int u,int fr,int side){//find the circle
	num[side]++;vis[u]=1;type[u]=side;
	for(int i=head[u];i;i=e[i].next){
		if(vis[e[i].t]==0){
			DFS2(e[i].t,u,side^1);
		}else{
			A=u;B=e[i].t;del=i;
		}
	}
}
int n,m;
void solve1(){
	del=0;
	DFS(1,0);
	if(delta[1]!=0) puts("-1"),exit(0);
}
void solve2(){
	DFS2(1,0,0);
	if(type[A]==type[B]){//odd
		int t=(num[type[A]^1]-num[type[A]])/2;
		ini[A]+=t;ini[B]+=t;
		DFS(A,0);
		ans+=abs(t);
	}else{//even
		if(num[0]!=num[1]) puts("-1"),exit(0);
		ax[B]=-1;
		DFS(A,0);
		assert(ax[A]==1);
		assert(delta[A]==0);
		vector<int> v;
		for(int i=1;i<=n;i++) v.push_back(-ax[i]*delta[i]);
		sort(v.begin(),v.end());
		for(int i=0,mid=v[v.size()/2];i<v.size();i++) ans+=abs(mid-v[i]);
	}
}
int main(){
	scanf("%d%d",&n,&m);ecnt=1;
	if(n%2!=0) return puts("-1"),0; 
	for(int i=1,u,v;i<=m;i++) scanf("%d%d",&u,&v),addedge(u,v);
	for(int i=1;i<=n;i++) ini[i]+=1;
	if(m==n-1) solve1();
	else solve2();
	cout<<ans<<endl;
	return 0;
}