#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long

struct node { ll t,next; }a[200010];

ll head[100010],fa[100010],num[100010],h1[100010],h2[100010],st[100010],n,m,u,v,tot,top,ans;
bool bo[100010],flag;

inline ll rd()
{
	ll x=0;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar());
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x;
}

inline void add(ll x,ll y) { a[++tot].t=y;a[tot].next=head[x];head[x]=tot; }

inline ll find(ll x)
{
	if (!fa[x]) return x;
	ll t=find(fa[x]);
	num[x]=num[x]^num[fa[x]];
	return fa[x]=t;
}

inline void pre(ll x,ll y)
{
	bo[x]=1;
	for (ll i=head[x];i;i=a[i].next)
	{
		ll t=a[i].t;
		if (t==y) continue;
		if (bo[t]) { u=x;v=t;if (num[u]==num[v]) flag=true; }
		else { num[t]=num[x]^1;pre(t,x); }
	}
}

inline void dfs(ll x,ll y)
{
	for (ll i=head[x];i;i=a[i].next)
	{
		ll t=a[i].t;
		if (t==y) continue;
		if (x==u&&t==v) continue;
		if (x==v&&t==u) continue;
		dfs(t,x);
		h1[x]+=h1[t];h2[x]+=h2[t];
	}
}

int main()
{
	n=rd();m=rd();tot=0;
	for (ll i=1;i<=m;i++)
	{
		ll x=rd(),y=rd();
		add(x,y);add(y,x);
	}
	num[1]=1;flag=false;pre(1,0);
	for (ll i=1;i<=n;i++)  h2[i]=(num[i])?1:-1;
	ll hh=0;
	for (ll i=1;i<=n;i++) hh+=h2[i];
	if (m==n-1) { if (hh) { puts("-1");return 0; } }
	else
	{
		if (flag)
		{
			if (hh&1) { puts("-1");return 0; }
			h2[u]-=(hh>>1);h2[v]-=(hh>>1);
			ans+=(ll)abs(hh>>1);
		}
		else
		{
			if (hh) { puts("-1");return 0; }
			h1[u]=1;h1[v]=-1;
		}
	}
	dfs(1,0);
	for (ll i=1;i<=n;i++)
	{
		if (!h1[i]) ans+=(ll)abs(h2[i]);
		else
		{
			if (h1[i]) h2[i]=-h2[i];
			st[++top]=h2[i];
		}
	}
	st[++top]=0;
	sort(st+1,st+top+1);
	hh=st[(top+1)>>1];
	for (ll i=1;i<=top;i++) ans+=(ll)abs(st[i]-hh);
	printf("%lld\n",ans);
	return 0;
}