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)

/*mjytxdy!*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define ull unsigned long long
#define ldb long double
#define pb push_back
#define fir first
#define sec second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define inl inline
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define travel(i,u) for(int i=head[u];i;i=e[i].next)
#define lb(x) ((x)&(-(x)))
#define lson root<<1
#define rson root<<1|1
#define Mid (l+r>>1)
#define debug(x) cout<<#x":"<<x<<'\n'
#define myput puts("QaQ")
#define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
const int MAXN = 1e5+10;
int n,m,u,v,x=1,y,cnt,vis[MAXN],sum[MAXN],fa[MAXN],head[MAXN],f[MAXN];
ll ans;
struct Edge{int to,next;}e[MAXN<<1];
inl char read(){
	static const int IO_LEN=1024*1024;
	static char buf[IO_LEN],*ioh,*iot;
	if(ioh==iot){
		iot=(ioh=buf)+fread(buf,1,IO_LEN,stdin);
		if(ioh==iot) return -1;
	}
	return *ioh++;
}
template<class T>inl void read(T &x){
	static int iosig;static char ioc;
	for(iosig=0,ioc=read();!isdigit(ioc);ioc=read()) if(ioc=='-') iosig=1;
	for(x=0;isdigit(ioc);ioc=read())x=(x<<1)+(x<<3)+(ioc^'0');
	if(iosig) x=-x;
}
inl void add(int u,int v){
	e[++cnt]=(Edge){v,head[u]},head[u]=cnt;
}
inl void dfs(int now,int dep,int father){
	sum[now]=dep&1?1:-1,fa[now]=father;
	travel(i,now){
		if(e[i].to==father) continue;
		dfs(e[i].to,dep+1,now);
		sum[now]+=sum[e[i].to];
	}
}
inl int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int main(){
//	freopen("in.txt","r",stdin);
	read(n),read(m);
	rep(i,1,n)f[i]=i;
	rep(i,1,m){
		read(u),read(v);
		if(find(u)!=find(v))add(u,v),add(v,u),f[find(u)]=find(v);
		else x=u,y=v;
	}
	dfs(x,1,0);
	if(m==n-1) if(sum[x]) return puts("-1")&0;
	else{
		vector<pii> mjy;int now=y;
		while(now) mjy.pb({sum[now],now}),now=fa[now];
		if(mjy.size()&1) {
			if(sum[x]&1) return puts("-1")&0;
			for(auto i:mjy)sum[i.sec]-=sum[x]>>1;
		}else {
			if(sum[x]) return puts("-1")&0;
			sort(all(mjy));
			int mid=mjy[mjy.size()>>1].fir;
			for(auto i:mjy)sum[i.sec]-=mid;
		}
	}
	rep(i,1,n)ans+=abs(sum[i]);
		printf("%lld\n",ans);
	return 0;
}