#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define me(a,x) memset(a,x,sizeof a)
using namespace std;
typedef long long LL;
inline int read(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0' || ch>'9'){if(ch=='-')f=-1; ch=getchar();}
	while(ch>='0' && ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}
	return x*f;
}
const int N=100005;
struct E{int y,next;}a[N<<1]; int len,first[N];
void ins(int x,int y){
	a[++len]=(E){y,first[x]},first[x]=len;
	a[++len]=(E){x,first[y]},first[y]=len;
}
int u,v,c[N],o,f[N],g[N],p[N],pl; LL ans;
void dfs(int x,int fa){
	for(int k=first[x];k;k=a[k].next){
		int y=a[k].y; if(y==fa)continue;
		if(!c[y]){c[y]=-c[x]; dfs(y,x);}
		else u=x,v=y,o=c[x]==c[y];
	}
}
void dp(int x,int fa){
	f[x]=c[x];
	for(int k=first[x];k;k=a[k].next){
		int y=a[k].y;
		if(y==fa ||(x==u&&y==v)||(x==v&&y==u))continue;
		dp(y,x);
		f[x]+=f[y],g[x]+=g[y];
	}
}
int main()
{
	int T=1;
	while(T--){
		int n=read(),m=read(),x,y,i;
		u=v=len=0; me(first,0);
		for(i=1;i<=n;i++)c[i]=f[i]=g[i]=0;
		for(i=1;i<=m;i++)
			x=read(),y=read(),ins(x,y);
		ans=0; 
		c[1]=1; dfs(1,0);
		if(m==n-1){
			dp(1,0);
			if(f[1]){printf("-1\n"); continue;}
			for(i=1;i<=n;i++)ans+=abs(f[i]);
			printf("%lld\n",ans);
		}
		else if(o){
			dp(1,0);
			if(f[1]&1){printf("-1\n"); continue;}
			int t=f[1]>>1;
			ans+=abs(t),c[u]-=t,c[v]-=t;
			dp(1,0);
			for(i=1;i<=n;i++)ans+=abs(f[i]);
			printf("%lld\n",ans);
		}
		else{
			g[u]++,g[v]--; dp(1,0);
			if(f[1]){printf("-1\n"); continue;}
			pl=1,p[1]=0;
			for(i=1;i<=n;i++)
				if(!g[i])ans+=abs(f[i]);
				else if(g[i]<0)p[++pl]=f[i];
				else p[++pl]=-f[i];
			sort(p+1,p+1+pl);
			int t=p[(pl+1)>>1];
			for(i=1;i<=pl;i++)ans+=abs(p[i]-t);
			printf("%lld\n",ans);
		}
	}
	return 0;
}