#include<bits/stdc++.h>
using namespace std;
#define N 1111116
#define next nxt

int n,m,tx,ty,x[N],y[N],next[N],p[N],f[N],c[N],s[N],k[N];
vector<int> q;

void travel(int x){
	for (int t=p[x];t;t=next[t])
		if (y[t]!=f[x]){
			if (!c[y[t]])
			f[y[t]]=x,c[y[t]]=-c[x],travel(y[t]);
			else tx=x,ty=y[t];
		}
}

void solve(int x){
	s[x]=c[x];
	for (int t=p[x];t;t=next[t])
		if (y[t]!=f[x]&&!(x==tx&&y[t]==ty)&&!(x==ty&&y[t]==tx))
		solve(y[t]),s[x]+=s[y[t]],k[x]+=k[y[t]];
}

int read(){
	int x=0,f=1;char ch=getchar();
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-f;
	for (;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
	return x*f;
}

int main(){
	n=read();m=read();
	for (int i=1;i<=m;i++){
		x[2*i-1]=y[2*i]=read();
		y[2*i-1]=x[2*i]=read();
		next[2*i-1]=p[x[2*i-1]];p[x[2*i-1]]=2*i-1;
		next[2*i]=p[x[2*i]];p[x[2*i]]=2*i;
	}
	c[1]=1;travel(1);long long ans=0;
	if (m==n-1){
		solve(1);
		if (s[1]) {printf("-1\n");return 0;};
		for (int i=1;i<=n;i++) ans+=abs(s[i]);
	}
	else if (c[tx]==c[ty]){
		solve(1);
		if (s[1]%2) {printf("-1\n");return 0;};
		c[tx]-=s[1]/2;c[ty]-=s[1]/2;
		ans=abs(s[1]/2);solve(1);
		for (int i=1;i<=n;i++) ans+=abs(s[i]);
	}
	else {
		k[tx]=1;k[ty]=-1;solve(1);
		if (s[1]) {printf("-1\n");return 0;};
		for (int i=1;i<=n;i++)
			if (k[i]==0) ans+=abs(s[i]);
			else q.push_back(s[i]*k[i]);
		q.push_back(0);
		sort(q.begin(),q.end());
		for (int i=0,j=(int)q.size()-1;i<j;i++,j--)
			ans+=q[j]-q[i];
	}
	printf("%lld\n",ans);
	return 0;
}