#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(e[i].t==fr) continue;
		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++) 
			if(ax[i]!=0)
				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;
}