#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100005
#define LL long long
#define rep(i,j,k) for(i=j;i<=k;++i)
struct E{
	int to,nxt;
	bool canbe;
}ed[N<<1];
int a,b,top,n,m,i,j,lca,u,v,sum,num;
int dep[N],head[N],val[N],s[N],fa[N],q[N];
LL ans;
void read(int &p){
	p=0; char x=getchar();
	while(x<'0' || x>'9') x=getchar();
	while(x>='0'&&x<='9'){p=p*10+x-'0'; x=getchar();}
}
void add(int u,int v){
	top++; ed[top]=(E){v,head[u]}; head[u]=top; ed[top].canbe=true;
}
void dfs(int x){
	int i;
	for(i=head[x];i;i=ed[i].nxt)
	if(!dep[ed[i].to]){
		dep[ed[i].to]=dep[x]+1;
		fa[ed[i].to]=x;
		dfs(ed[i].to);
	}else 
	if(fa[x]!=ed[i].to){
		ed[i].canbe=false;
		a=ed[i].to; b=x;
	}
}
void solve(int x){
	int i;
	s[x]=val[x];
	for(i=head[x];i;i=ed[i].nxt)
	if((ed[i].to!=fa[x])&&(ed[i].canbe)){
		solve(ed[i].to);
		s[x]+=s[ed[i].to];
	}
}
int main(){
	read(n); read(m);
	rep(i,1,m){
		read(a); read(b);
		add(a,b); add(b,a);
	}
	dep[1]=1; a=b=0;
	dfs(1);
	rep(i,1,n)
	if(dep[i]&1) num++,val[i]=1;
	else num--,val[i]=-1;
	if(n-1==m){
		if(num) { puts("-1"); return 0; }
		solve(1);
		rep(i,1,n) ans+=abs(s[i]);
		printf("%lld\n",ans);
		return 0;
	}
	u=a; v=b;
	if(dep[a]<dep[b]) swap(a,b);
	while(dep[a]>dep[b]) sum++,a=fa[a];
	while(a!=b){ a=fa[a]; b=fa[b]; }
	lca=a;
	if(!(sum&1)){//odd
		if(num&1) { puts("-1"); return 0; }	
		if(num>0) { val[u]-=(num>>1); val[v]-=(num>>1); }
		if(num<0) { val[u]+=((-num)>>1); val[v]+=((-num)>>1); }
		ans+=(abs(num)>>1);
		solve(1);
		rep(i,1,n) ans+=abs(s[i]);
		printf("%lld\n",ans);
		return 0;
	}
	if(num) { puts("-1"); return 0; }
	a=u; b=v; top=1; q[1]=0;
	solve(1);
	rep(i,1,n) ans+=abs(s[i]);
	while(a!=lca){
		top++; q[top]=(-s[a]); ans-=abs(s[a]);
		a=fa[a];
	}
	while(b!=lca){
		top++; q[top]=s[b]; ans-=abs(s[b]);
		b=fa[b];
	}
	sort(q+1,q+1+top);
	num=q[((top+1)>>1)];
	rep(i,1,top) ans+=abs(q[i]-num);
	printf("%lld\n",ans);
	return 0;
}