#include<cstdio>
#include<algorithm>
using namespace std;
inline int read()
{
	int x;char c;
	while((c=getchar())<'0'||c>'9');
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return x;
}
#define MN 100000
struct edge{int nx,t;}e[MN*2+5];
int h[MN+5],en,s[MN+5],d[MN+5],z[MN+5],q[MN+5],qn;
long long ans;
inline void ins(int x,int y)
{
	e[++en]=(edge){h[x],y};h[x]=en;
	e[++en]=(edge){h[y],x};h[y]=en;
}
void get(int x,int fa)
{
	z[d[x]=d[fa]+1]=x;
	for(int i=h[x];i;i=e[i].nx)if(e[i].t!=fa)
		if(d[e[i].t]){if(!qn)for(int j=d[e[i].t];j<=d[x];++j)q[++qn]=z[j];}
		else get(e[i].t,x);
}
inline int Z(int x){return x<0?-x:x;}
void dfs(int x,int fa)
{
	for(int i=h[x];i;i=e[i].nx)if(d[e[i].t]>=0&&e[i].t!=fa)s[e[i].t]=-s[x];
	for(int i=h[x];i;i=e[i].nx)if(d[e[i].t]>=0&&e[i].t!=fa)dfs(e[i].t,x),s[x]+=s[e[i].t];
	if(d[x]>=0)ans+=Z(s[x]);
}
int main()
{
	int n,m,i,x;
	n=read();m=read();
	for(i=1;i<=m;++i)ins(read(),read());
	if(m<n){dfs(s[1]=1,0);return 0*printf("%d",s[1]?-1:ans);}
	get(1,0);
	for(i=1;i<=qn;++i)d[q[i]]=-1;
	for(i=1;i<=qn;++i)s[q[i]]=i&1?1:-1,dfs(q[i],0);
	for(i=1;i<=qn;++i)d[i]=d[i-1]+s[q[i]];
	if(qn&1?d[qn]&1:d[qn])return 0*puts("-1");
	if(qn&1)x=d[qn]/2;else nth_element(d+1,d+qn/2,d+qn+1),x=d[qn/2];
	for(i=1;i<=qn;++i)ans+=Z(d[i]-x);
	printf("%lld",ans);
}