#include<cstdio>
#include<cmath>
#include<algorithm>
#define neko 100010
#define f(i,a,b) for(register int i=(a);i<=(b);i=-(~(i)))
int n,m,t,ciru,cirv,ans,tp,flag;
struct node
{int v,nex;}e[neko<<1];
typedef int arr[neko];
arr num,dep,head,fa,s;
void add(int x,int y)
{
	e[++t].v=y,e[t].nex=head[x],head[x]=t;
	e[++t].v=x,e[t].nex=head[y],head[y]=t;
}
void dfs(int u)
{
	num[u]=((dep[u]=dep[fa[u]]+1)&1)?1:-1;
	for(register int i=head[u],v=e[i].v;i;i=e[i].nex,v=e[i].v)
	{
		if(v^fa[u])
		{
			if(dep[v]){if(!flag)ciru=u,cirv=v,flag=1;}
			else fa[v]=u,dfs(v),num[u]+=num[v];
		}
	}
}
void solve_odd()
{
	if(num[1]&1){ans=-1;return;}
	int fix=-num[1]/2,x;
	ans=abs(fix);
	for(x=ciru;x;x=fa[x])num[x]+=fix;
	for(x=cirv;x;x=fa[x])num[x]+=fix;
	f(i,1,n)ans+=abs(num[i]);
}
void solve_even()
{
	if(num[1]){ans=-1;return;}
	for(int x=ciru;x^cirv;x=fa[x])s[++tp]=num[x],dep[x]=0;
	//网上一些Solution没flag标记，可能最终会导致最后找到的那条环边一定是从上往下的，所以他们写的从cirv跳到ciru才是正确的。
	int mid=(tp+1)>>1,now;
	std::nth_element(s+1,s+mid,s+tp+1);
	if(tp&1)now=s[mid];
	else now=(s[mid]+s[mid+1])/2;
	ans=abs(now);
	f(i,1,n)if(dep[i])ans+=abs(num[i]);
	f(i,1,tp)ans+=abs(s[i]-now);
}
int main()
{
	int x,y;
	scanf("%d%d",&n,&m);
	f(i,1,m)scanf("%d%d",&x,&y),add(x,y);
	dfs(1);
	if(m==n-1)if(num[1])ans=-1;else f(i,1,n)ans+=abs(num[i]);
	else if((dep[ciru]-dep[cirv]+1)&1)solve_odd();
	else solve_even();
	int xx[10]={0,1,4,2,3};
	return printf("%d\n",ans),0;
}