#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=100005,maxe=maxn<<1;
int n,m,fa[maxn],U,V,flg,w[maxn],s[maxn],Q[maxn];
int e,son[maxe],nxt[maxe],lnk[maxn];
long long ans;
inline char nc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
	int ret=0;bool f=0;char ch=nc();
	while(ch>'9'||ch<'0') f^=ch=='-',ch=nc();
	while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=nc();
	return f?-ret:ret;
}
int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
inline void add_e(int x,int y){son[++e]=y;nxt[e]=lnk[x];lnk[x]=e;}
inline int abs(int x){return x<0?-x:x;}
inline long long solve(int x){
	w[U]+=x,w[V]+=x*flg;
	memset(s,0,sizeof s);ans=0;
	for (int i=n;i;i--)
	s[Q[i]]+=w[Q[i]],ans+=abs(s[Q[i]]),s[fa[Q[i]]]+=s[Q[i]];
	w[U]-=x,w[V]-=x*flg;
	return ans+abs(x);
}
int main(){
	n=read(),m=read();
	if(n&1){puts("-1");return 0;}
	for (int i=1;i<=n;i++) fa[i]=i;
	for (int i=1;i<=m;i++){
		int u=read(),v=read();
		int fu=getfa(u),fv=getfa(v);
		if(fu==fv) U=u,V=v;else add_e(u,v),add_e(v,u),fa[fu]=fv;
	}
	int hed=fa[1]=0,til=Q[1]=w[1]=1;
	while(hed^til)
	for (int i=lnk[Q[++hed]];i;i=nxt[i]) if(son[i]^fa[Q[hed]])
	Q[++til]=son[i],fa[son[i]]=Q[hed],w[son[i]]=-w[Q[hed]];
	solve(0);flg=w[U]==w[V]?1:-1;
	if(s[1]&&((s[1]&1)==1||!U||w[U]!=w[V])){puts("-1");return 0;}
	if(!U) printf("%lld\n",solve(0));else
	if(w[U]==w[V]) printf("%lld\n",solve(-s[1]/2));else{
		int L=-n,R=n;
		while(L<R){
			int midl=floor(1.0*(L+L+R)/3),midr=ceil(1.0*(L+R+R)/3-1e-9);
			if(solve(midl)<solve(midr)) R=midr-1;else L=midl+1;
		}
		printf("%lld\n",solve(L));
	}
	return 0;
}