#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+5;

int head[N],nxt[N<<1],to[N<<1],siz[N],z[N],k[N];
int n,m,num,X,Y,odd,sum,tot,mid;
LL ans;

int read()
{
    int x=0,p=1; char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') p=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*p;
}

void add(int x,int y)
{
	nxt[++num]=head[x],to[num]=y,head[x]=num;
}

void dfs1(int x,int fa)
{
	for(int i=head[x]; i ;i=nxt[i])
	{
		int y=to[i];
		if(y==fa) continue ;	
		if(siz[y])
		{
			if(siz[y]==siz[x]) odd=1;
			X=x,Y=y;
		}
		else
		{
			siz[y]=-siz[x];
			dfs1(y,x);
		}
	}
}

void dfs2(int x,int fa)
{
	for(int i=head[x]; i ;i=nxt[i])
	{
		int y=to[i];
		if(y==fa || (x==X&&y==Y) || (x==Y&&y==X))
			continue ;
		dfs2(y,x);
		siz[x]+=siz[y];
		k[x]+=k[y];
	}
}

int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);

	n=read(),m=read();
	for(int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		add(x,y),add(y,x);
	}

	siz[1]=1;
	dfs1(1,0);
	for(int i=1;i<=n;i++) sum+=siz[i];

	if(n-1==m)
	{ if(sum) return printf("-1\n"),0; }
	else
		if(odd)
		{
			if(sum&1) return printf("-1\n"),0;
			ans+=abs(sum>>1); 
			siz[X]-=sum>>1,siz[Y]-=sum>>1;
		}
		else
		{
			if(sum) return printf("-1\n"),0;
			k[X]=1,k[Y]=-1;
		}

	dfs2(1,0);
	for(int i=1;i<=n;i++)
		if(k[i]) z[++tot]=k[i]*siz[i];
		else ans+=abs(siz[i]);
	z[++tot]=0;
	sort(z+1,z+tot+1);
	mid=z[tot+1>>1];
	for(int i=1;i<=tot;i++) ans+=abs(z[i]-mid);

	printf("%lld\n",ans);
    return 0;
}
