#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define M 400020
using namespace std;
int read(){
	int nm=0,fh=1; char cw=getchar();
	for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
	for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
	return nm*fh;
}
int n,m,col[M],vis[M],u,v,sum[M],cal[M];
int fs[M],nt[M],last[M],to[M],tmp,top,S[M];
void link(int x,int y){nt[tmp]=fs[x],fs[x]=tmp,to[tmp++]=y;}
void init(int x,int last){
	if(!vis[x]) col[x]=-col[last],vis[x]=1;
	sum[x]=col[x];
	for(int i=fs[x];i!=-1;i=nt[i]){
		if(to[i]==last) continue;
		if(x==v&&to[i]==u) continue;
		if(x==u&&to[i]==v) continue;
		if(vis[to[i]]==1&&!u&&m==n) u=x,v=to[i];
		else init(to[i],x),sum[x]+=sum[to[i]];
	}
}
void dfs(int x,int last){
	for(int i=fs[x];i!=-1;i=nt[i]){
		if(to[i]==last||(x==u&&to[i]==v)||(x==v&&to[i]==u)) continue;
		dfs(to[i],x),cal[x]+=cal[to[i]];
	}
}
int getans(){
	if(n&1) return -1;
	int res=0; col[0]=-1,u=v=0,init(1,0);
	if(m==n-1){
		if(sum[1]) return -1;
		for(int i=2;i<=n;i++) res+=abs(sum[i]);
		return res;
	}
	if(col[u]==col[v]){
		col[u]-=sum[1]/2,col[v]-=sum[1]/2,res+=abs(sum[1]/2),init(1,0);
		for(int i=1;i<=n;i++) res+=abs(sum[i]); return res;
	}
	else if(sum[1]) return -1;
	cal[u]=1,cal[v]=-1,dfs(1,0),S[top=1]=0;
	for(int i=1;i<=n;i++){
		if(!cal[i]) res+=abs(sum[i]);
		else S[++top]=cal[i]*sum[i];
	}
	nth_element(S+1,S+((top+1)>>1),S+top+1);
	for(int i=1;i<=top;i++) res+=abs(S[(top+1)>>1]-S[i]);
	return res;
}
int main(){
	n=read(),m=read(),memset(fs,-1,sizeof(fs));
	for(int i=1;i<=m;i++) u=read(),v=read(),link(u,v),link(v,u);
	printf("%d\n",getans()); return 0;
}