#include<bits/stdc++.h>
#define re register
#define inc(i,j,k) for(re int i=j;i<=k;i++)
using namespace std;
const int N=1e5+5;
int a[N],fa[N],head[N],cnt,b[N],p;
int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
struct egde{
	int to,nxt;
}e[N<<1];
void add(int a,int b){
	e[++cnt].to=b;
	e[cnt].nxt=head[a];
	head[a]=cnt;
}
void dfs(int u,int type){
	a[u]=type;
	for(re int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==fa[u]) continue;
		fa[v]=u;
		dfs(v,-type);
		a[u]+=a[v];
	}
}
int main(){
	int n,m,rt=1,rs=1,ans=0;
	scanf("%d%d",&n,&m);
	inc(i,1,n) fa[i]=i;
	inc(i,1,m){
		int x,y;
		scanf("%d%d",&x,&y);
		if(getfa(x)!=getfa(y)) fa[fa[x]]=fa[y];
		else{rt=x,rs=y;continue;}
		add(x,y),add(y,x);
	}
	fa[rt]=0,dfs(rt,1);
	if(n==m){
		for(re int i=rs;i;i=fa[i]) b[++p]=a[i];
		if(p&1){
			if(a[rt]&1) return puts("-1"),0;
			for(re int i=rs;i;i=fa[i]) a[i]-=a[rt]>>1;
		}
		else{
			if(a[rt]) return puts("-1"),0;
			sort(b+1,b+p+1);
			for(re int i=rs;i;i=fa[i]) a[i]-=b[p>>1]; 
		}
	}
	else if(a[rt]) return puts("-1"),0;
	inc(i,1,n) ans+=abs(a[i]);
	printf("%d",ans);
}