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 <cstdio>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <algorithm>
#define MAXN 100010
using namespace std;

struct edge{
	int to,next;
	edge(int _to=0,int _next=0):to(_to),next(_next){}
}e[MAXN<<1];

int n,m,r1,r2;
int g[MAXN],nume,pre[MAXN],tag[MAXN];
int fa[MAXN];
int d[MAXN],delta;
int f1[MAXN],s1[MAXN];
int f2[MAXN],s2[MAXN];

void addEdge(int u,int v){
	e[nume]=edge(v,g[u]);
	g[u]=nume++;
}

int getfa(int x){
	if(fa[x]==x) return x;
	return fa[x]=getfa(fa[x]);
}

void dfs0(int x,int p){
	pre[x]=p;
	for(int i=g[x];~i;i=e[i].next)
		if(e[i].to^p){
			d[e[i].to]=!d[x];
			dfs0(e[i].to,x);
		}
}

void dfs1(int x,int p){
	if(d[x]) s1[x]=1;
	else s1[x]=-1;
	for(int i=g[x];~i;i=e[i].next)
		if(e[i].to^p){
			dfs1(e[i].to,x);
			s1[x]+=s1[e[i].to];
			f1[x]+=f1[e[i].to]+abs(s1[e[i].to]);
		}
}

void dfs2(int x,int p){
	if(d[x]) s2[x]=1;
	else s2[x]=-1;
	for(int i=g[x];~i;i=e[i].next)
		if(e[i].to!=p && !tag[e[i].to]){
			dfs2(e[i].to,x);
			s2[x]+=s2[e[i].to];
			f2[x]+=f2[e[i].to]+abs(s2[e[i].to]);
		}
}

int main(){
#ifdef DEBUG
	freopen("F.in","r",stdin);
#endif
	memset(g,-1,sizeof g);
	scanf("%d%d",&n,&m);
	if(n&1){
		puts("-1");
		return 0;
	}
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=1;i<=m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		if(getfa(u)!=getfa(v)){
			fa[fa[u]]=fa[v];
			addEdge(u,v);
			addEdge(v,u);
		}else{
			r1=u; r2=v;
		}
	}
	if(!r1) r1=1;
	dfs0(r1,0);
	if(r2){
		for(int i=r2;i;i=pre[i])
			tag[i]=1;
	}
	int c1=0;
	for(int i=1;i<=n;i++)
		if(d[i]) c1++;
	if((n-2*c1)%4!=0){
		puts("-1");
		return 0;
	}
	delta=(n-2*c1)/4;
	if(!r2){
		if(delta!=0){
			puts("-1");
			return 0;
		}
		dfs1(r1,0);
		printf("%d\n",f1[r1]);
	}else if(d[r1]!=d[r2]){
		if(delta!=0){
			puts("-1");
			return 0;
		}
		static int b[MAXN];
		int numb=0;
		int ans=0,temp=0;
		for(int i=1;i<=n;i++)
			if(tag[i]){
				dfs2(i,0);
				ans+=f2[i];
				temp+=s2[i];
				b[++numb]=-temp;
			}
		sort(b+1,b+numb+1);
		int d0=b[(numb+1)/2];
		for(int i=1;i<=numb;i++)
			ans+=abs(d0-b[i]);
		printf("%d\n",ans);
	}else{
		assert(0);
	}
	return 0;
}