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<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 100000
using namespace std;
typedef long long LL;
struct node
{
	int to;
	node *nxt;
}edges[MAXN*2+5];
node *ncnt,*Adj[MAXN+5];
int N,M,spx,spy;
LL sum[MAXN+5];
LL cirlen,dist[MAXN+5],xs[MAXN+5];
LL stk[MAXN+5],t;
bool vis[MAXN+5];
void Init()
{
	ncnt=&edges[0];
}
void AddEdge(int u,int v)
{
	node *p=++ncnt;
	p->to=v;
	p->nxt=Adj[u];
	Adj[u]=p;
	
	node *q=++ncnt;
	q->to=u;
	q->nxt=Adj[v];
	Adj[v]=q;
}
void DFS(int u,int fa)
{
	vis[u]=true;
	for(node *p=Adj[u];p!=NULL;p=p->nxt)
	{
		int v=p->to;
		if(v==fa)
			continue;
		if(vis[v]==true)
		{
			spx=u,spy=v;
			cirlen=dist[v]-dist[u]+1;
			continue;
		}
		dist[v]=dist[u]+1;
		DFS(v,u);
	}
}
void GetXs(int u,int fa)
{
	vis[u]=true;
	for(node *p=Adj[u];p!=NULL;p=p->nxt)
	{
		int v=p->to;
		if(v==fa||vis[v]==true)
			continue;
		GetXs(v,u);
		xs[u]+=xs[v];
		sum[u]+=sum[v];
	}
}
int main()
{
//	freopen("print.in","r",stdin);
//	freopen("print.out","w",stdout);
	Init();
	scanf("%d %d",&N,&M);
	int u,v;
	for(int i=1;i<=M;i++)
	{
		scanf("%d %d",&u,&v);
		AddEdge(u,v);
	}
	dist[1]=0;
	DFS(1,-1);
	LL ans=0,tot=0;
	for(int i=1;i<=N;i++)
	{
		if(dist[i]%2==0)
			sum[i]=1LL;
		else
			sum[i]=-1LL;
		tot+=sum[i];
	}
	if(N-1==M)
	{
		if(tot!=0)
		{
			printf("-1\n");
			return 0;
		}
	}
	else
	{
		if(cirlen%2==1)
		{
			if(tot%2==1)
			{
				printf("-1\n");
				return 0;
			}
			sum[spx]-=tot/2;
			sum[spy]-=tot/2;
			ans+=abs(tot)/2;
		}
		else
		{
			if(tot!=0)
			{
				printf("-1\n");
				return 0;
			}
			xs[spx]=1LL;
			xs[spy]=-1LL;
		}
	}
	memset(vis,0,sizeof(vis));
	GetXs(1,-1);
	for(int i=1;i<=N;i++)
	{
		if(xs[i]==0)	ans+=abs(sum[i]);
		else
		{
			if(xs[i]==1)
				sum[i]=-sum[i];
			stk[++t]=sum[i];
		}
	}
	stk[++t]=0;
	sort(stk+1,stk+1+t);
	LL k=stk[(t+1)/2];
	for(int i=1;i<=t;i++)
		ans+=1LL*abs(stk[i]-k);
	printf("%lld\n",ans);
	return 0;
}