#include<bits/stdc++.h>
const int MN=200000+5,P=20101009;
using namespace std;
typedef long long ll;
template<typename T>inline T&IN(T&in){
	in=0;char c=getchar();int f=1;
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c))in=in*10+c-'0',c=getchar();
	return in*=f;
}
int n,m,ans;
int fa[MN],dep[MN],f[MN],ls[MN];
int nxt[MN],to[MN],h[MN],cnt;
void add(int u,int v){to[cnt]=v,nxt[cnt]=h[u],h[u]=cnt++;}
void cir(int u,int v){while(u!=v)ls[++ls[0]]=u,u=fa[u];}
void dfs(int u,int pa){
	fa[u]=pa;dep[u]=dep[pa]+1;f[u]=dep[u]%2?1:-1;
	for(int e=h[u],v=to[e];~e;e=nxt[e],v=to[e])if(v^pa&&dep[v]<dep[u]){
		if(dep[v]){cir(u,v);continue;}
		dfs(v,u),f[u]+=f[v];
	}
}
void work1(){
	if(f[1])return puts("-1"),void();
	for(int i=1;i<=n;++i)ans+=abs(f[i]);
	printf("%d\n",ans);
}
void work2(){
	if(f[1]%2)return puts("-1"),void();
	int del=-f[1]/2;ans+=abs(del);
	for(int u=ls[1];u;u=fa[u])f[u]+=del;
	for(int u=fa[ls[ls[0]]];u;u=fa[u])f[u]+=del;
	for(int i=1;i<=n;++i)ans+=abs(f[i]);
	printf("%d\n",ans);
}
void work3(){
	if(f[1])return puts("-1"),void();
	for(int i=1;i<=ls[0];++i)dep[ls[i]]=0,ls[i]=f[ls[i]];
	sort(ls+1,ls+ls[0]+1);int del=ls[ls[0]+1>>1];ans=abs(del);
	for(int i=1;i<=n;++i)if(dep[i])ans+=abs(f[i]);
	for(int i=1;i<=ls[0];++i)ans+=abs(ls[i]-del);
	printf("%d\n",ans);
}
void input(){
	IN(n),IN(m);memset(h,-1,sizeof h);
	for(int i=1,u,v;i<=m;++i)IN(u),IN(v),add(u,v),add(v,u);
	dfs(1,0);
	if(m==n-1)work1();
	else if(ls[0]%2)work3();
	else work2();
}
int main(){
	input();
	return 0;
}