#include<bits/stdc++.h>
typedef long long i64;
const int N=2e5+7;
int n,m;
int col[N],t[5],odd=0,p1,p2,s1[N],on[N];
i64 abs(i64 x){return x>0?x:-x;}
struct edge{
	int to,nx;
}e[N*2];
int e0[N],ep=2,fa[N];
void f1(int w,int c){
	++t[col[w]=c];
	s1[w]=3-c*2;
	for(int i=e0[w];i;i=e[i].nx){
		int u=e[i].to;
		if(!u)continue;
		if(!col[u]){
			fa[u]=w;
			e[i^1].to=0;
			f1(u,c^3);
			s1[w]+=s1[u];
		}else{
			e[i].to=e[i^1].to=0;
			odd=col[w]==col[u];
			p1=w,p2=u;
		}
	}
}
i64 s=0;
int ps[N],pp=0;
void tree_cal(){
	for(int i=2;i<ep;++i){
		int u=e[i].to;
		if(u)s+=abs(s1[u]);
	}
}
i64 solve(){
	scanf("%d%d",&n,&m);
	if(n&1)return -1;
	for(int i=1,a,b;i<=m;++i){
		scanf("%d%d",&a,&b);
		e[ep]=(edge){b,e0[a]};e0[a]=ep++;
		e[ep]=(edge){a,e0[b]};e0[b]=ep++;
	}
	f1(1,1);
	if(n>m){
		if(t[1]!=t[2])return -1;
		tree_cal();
	}else if(odd){
		int a=(t[2]-t[1])/2;
		s+=abs(a);
		for(int w=p1;w;w=fa[w])s1[w]+=a;
		for(int w=p2;w;w=fa[w])s1[w]+=a;
		tree_cal();
	}else{
		if(t[1]!=t[2])return -1;
		for(int w=p1;w!=p2;w=fa[w])on[w]=1;
		ps[pp++]=0;
		for(int i=2;i<ep;++i){
			int u=e[i].to;
			if(u){
				if(on[u])ps[pp++]=s1[u];
				else s+=abs(s1[u]);
			}
		}
		std::nth_element(ps,ps+pp/2,ps+pp);
		int x=ps[pp/2];
		s+=abs(x);
		for(int i=0;i<pp;++i)s+=abs(ps[i]-x);
	}
	return s;
}
int main(){
	printf("%lld\n",solve());
	return 0;
}