#include<bits/stdc++.h>
#define N 200005
using namespace std;
int n,m,ly,s[N],b[N],vis[N],sum,tar,flag,a1[N];long long Ans;
int to[N*2],fst[N],nxt[N*2],hz[N*2],l=1;
void biu()
{
	puts("-1");
	exit(0);
}
void link(int x,int y)
{
	to[++l]=y;nxt[l]=fst[x];fst[x]=l;
	to[++l]=x;nxt[l]=fst[y];fst[y]=l;
}
struct T
{
	int x,y;
}a[N];
T operator+(T a,T b)
{
	return (T){a.x+b.x,a.y+b.y};
}
void Dfs(int x,int y,int f)
{
	a[x].y=y;b[x]=y^1;
	vis[x]=1;sum+=y;tar+=y^1;
	for (int i=fst[x];i;i=nxt[i])
		if (to[i]!=f)
		{
			if (!vis[to[i]])
				Dfs(to[i],y^1,x);
			else flag=i;
		}
}
void Get(int x,int y,int f)
{
	a[x].y=y+a1[x];b[x]=y^1;
	sum+=a[x].y;tar+=b[x];
	for (int i=fst[x];i;i=nxt[i])
		if (to[i]!=f&&!hz[i])
		{
			Get(to[i],y^1,x);
			a[x]=a[x]+a[to[i]];
			b[x]=b[x]+b[to[i]];
		}
}
int main()
{
	scanf("%d%d",&n,&m);
	if (n&1) biu();
	for (int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		link(x,y);
	}
	Dfs(1,1,0);
	if (flag)
	{
		int u=to[flag],v=to[flag^1];
	//	cout<<u<<'-'<<v<<endl;
	//	cout<<tar<<' '<<sum<<endl;
		if (a[u].y==a[v].y)
		{
			//puts("1%");
			if ((tar-sum)%2) biu();
			if (a[u].y) a1[u]+=(tar-sum)/2,a1[v]+=(tar-sum)/2;
			else a1[u]+=(tar-sum)/2,a1[v]+=(tar-sum)/2;
			Ans+=abs((tar-sum)/2);
		//	cout<<Ans<<endl;
		}
		else
		{
			//Ans=Get+x
			if (sum!=tar) biu();
			a[u].x++;a[v].x--;
			s[++ly]=0;
		}
		hz[flag]=hz[flag^1]=1;
	}
	else if (sum!=tar) biu();
	sum=tar=0;
	Get(1,1,0);
	//cout<<sum<<' '<<tar<<endl;
	for (int i=2;i<=n;i++)
	{
		//cout<<a[i].x<<' '<<a[i].y<<endl;
		a[i].y-=b[i];
		if (a[i].x) s[++ly]=a[i].x==1?a[i].y:-a[i].y;
		else Ans+=abs(a[i].y);
	}
	sort(s+1,s+ly+1);
	for (int i=1;i<=ly;i++)
		Ans+=abs(s[i]-s[(ly+1)/2]);
	printf("%lld\n",Ans);
}