/*
  mail: mleautomaton@foxmail.com
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
	int f=1,sum=0;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int N=100010;
int front[N],nxt[N<<1],to[N<<1],cnt,top,s[N],k[N];
void Add(int u,int v)
{
	to[++cnt]=v;nxt[cnt]=front[u];front[u]=cnt;
}
int dep[N],f[N],tot,n,m,len,U,V;ll ans;
void dfs(int u,int fa)
{
	dep[u]=dep[fa]+1;f[u]=dep[u]&1?1:-1;
	tot+=f[u];
	for(int i=front[u];i;i=nxt[i])
	{
		int v=to[i];
		if(v==fa)continue;
		if(dep[v])len=abs(dep[u]-dep[v])+1,U=u,V=v;
		else dfs(v,u);
	}
}
void dfs2(int u,int fa)
{
	for(int i=front[u];i;i=nxt[i])
	{
		int v=to[i];
		if(v==fa || (U==u && V==v) || (U==v && V==u))continue;
		dfs2(v,u);
		k[u]+=k[v];f[u]+=f[v];
	}
	if(!k[u])ans+=abs(f[u]);
	else s[++top]=k[u]*f[u];
}
int main()
{
	n=gi();m=gi();
	for(int i=1;i<=m;i++)
	{
		int u=gi(),v=gi();
		Add(u,v);Add(v,u);
	}
	dfs(1,1);
	if(m==n-1){if(tot!=0){puts("-1");return 0;}}
	if(len%2)
	{
		if(tot&1){puts("-1");return 0;}
		ans+=abs(tot/2);f[U]-=tot/2;f[V]-=tot/2;
	}
	else
	{
		if(tot){puts("-1");return 0;}
		k[U]=1,k[V]=-1;
	}
	dfs2(1,1);
	s[++top]=0;
	sort(s+1,s+top+1);
	for(int i=1;i<=top;i++)ans+=abs(s[i]-s[(top+1)/2]);
	printf("%lld\n",ans);
	return 0;
}
