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>
#define int long long
using namespace std;
int n,m,ans,val[100005],ban[100005];
vector<int>g[100005];
int dfs(int x,int p,int dep){
	int got=0;
	for(auto to:g[x])if(to!=p&&!ban[to]&&to!=-1){
		int v=dfs(to,x,dep^1);ans+=abs(v);got+=v;
	}
	if(dep&1)got-=val[x];
	else got+=val[x];
	return got;
}
void solve_tree(){
	if(dfs(1,-1,0)){
		cout<<-1<<endl;
		return;
	}
	cout<<ans<<endl;
}
vector<int>cyc;
bool vis[100005];
int par[100005],col[100005],cnt[2];
void go(int x,int p,int dep){
	par[x]=p;cnt[dep&1]++;col[x]=dep&1;vis[x]=1;
	for(auto to:g[x]){
		if(to!=p){
			if(vis[to]){
				if(cyc.size())continue;
				int cur=x;
				while(cur!=to){
					cyc.push_back(cur);
					cur=par[cur];
				}
				cyc.push_back(cur);
			}else{
				go(to,x,dep+1);
			}
		}
	}
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;cin>>x>>y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	for(int i=1;i<=n;i++)val[i]=1;
	if(m==n-1){
		solve_tree();
		return 0;
	}else{
		go(1,0,0);
		if(cyc.size()%2==0){
			//even circle
			for(auto vv:cyc)ban[vv]=1;
			vector<int>dat;
			for(auto vv:cyc){
				dat.push_back(dfs(vv,0,col[vv]));
			}
			for(int i=1;i<dat.size();i++)dat[i]+=dat[i-1];
			if(dat.back()){
				puts("-1");
				return 0;
			}
			sort(dat.begin(),dat.end());
			for(int i=0;i<dat.size()/2;i++)ans-=dat[i];
			for(int i=dat.size()/2;i<dat.size();i++)ans+=dat[i];
			cout<<ans<<endl;
		}else{
			//odd circle
			int x=cyc[0],y=cyc.back();
			int dif=cnt[col[x]]-cnt[1-col[x]];
			if(dif%2){
				puts("-1");
				return 0;
			}
			val[x]-=dif/2;val[y]-=dif/2;ans+=abs(dif/2);
			for(auto &v:g[x])if(v==y)v=-1;
			for(auto &v:g[y])if(v==x)v=-1;
			if(dfs(1,-1,0)){
				puts("-1");
				return 0;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}