#include <bits/stdc++.h>
#define fail {puts("-1");return 0;}
typedef unsigned ui;
constexpr ui N(1e5+1),M(N<<1);
ui head[N],e_cnt,to[M],nxt[M];
void addedge(ui u,ui v)
{
	to[++e_cnt]=v;
	nxt[e_cnt]=head[u];
	head[u]=e_cnt;
	return;
}
bool odd;
ui S,T;
int sz[N],k[N];
void DFS1(ui u,ui fa)
{
	for(ui e(head[u]),v(to[e]);e;v=to[e=nxt[e]])
		if(v^fa)
		{
			if(sz[v])
			{
				if(sz[u]==sz[v])
					odd=true;
				S=u,T=v;
			}
			else
			{
				sz[v]=-sz[u];
				DFS1(v,u);
			}
		}
	return;
}
void DFS2(ui u,ui fa)
{
	for(ui e(head[u]),v(to[e]);e;v=to[e=nxt[e]])
		if(v!=fa&&(u!=S||v!=T)&&(u!=T||v!=S))
		{
			DFS2(v,u);
			sz[u]+=sz[v];
			k[u]+=k[v];
		}
	return;
}
signed main(void)
{
	ui n,m;
	scanf("%u%u",&n,&m);
	for(ui i(0),u,v;i<m;++i)
	{
		scanf("%u%u",&u,&v);
		addedge(u,v);
		addedge(v,u);
	}
	sz[1]=1,DFS1(1,0);
	int sum(std::accumulate(sz+1,sz+n+1,0));
	ui ans(0);
	if(m<n&&sum) fail
	if(m==n)
	{
		if(odd)
		{
			if(sum&1) fail
			ans+=abs(sum/2);
			sz[S]-=sum/2,sz[T]-=sum/2;
		}
		else
		{
			if(sum) fail
			k[S]=1,k[T]=-1;
		}
	}
	DFS2(1,0);
	static int st[N];
	ui top(0);
	for(ui i(1);i<=n;++i)
		if(k[i])
			st[++top]=k[i]*sz[i];
		else
			ans+=abs(sz[i]);
	st[++top]=0,std::sort(st+1,st+top+1);
	int mid(st[(top+1)>>1]);
	for(ui i(1);i<=top;++i)
		ans+=abs(st[i]-mid);
	printf("%lld\n",ans);
	return 0;
}