#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
struct edges
{
	int to,next;
	bool mk;
}edge[202020];
int first[101010],now;
void adde(int x,int y)
{
	now++;
	edge[now].to=y;
	edge[now].next=first[x];
	edge[now].mk=false;
	first[x]=now;
}
int c[101010];
int fa[101010],dep[101010];
long long sum[101010];
int ex,ey;
void work(int p)
{
	for(int e=first[p];e;e=edge[e].next)
		if(edge[e].to!=fa[p])
		{
			if(c[edge[e].to])
			{
				ex=p;
				ey=edge[e].to;
			}
			else
			{
				c[edge[e].to]=-c[p];
				edge[e].mk=true;
				fa[edge[e].to]=p;
				dep[edge[e].to]=dep[p]+1;
				work(edge[e].to);
			}
		}
}
void work1(int p)
{
	sum[p]=c[p];
	for(int e=first[p];e;e=edge[e].next)
		if(edge[e].mk)
		{
			work1(edge[e].to);
			sum[p]+=sum[edge[e].to];
		}
}
int q[101010],qn;
void work2(int x,int y)
{
	while(x!=y)
	{
		if(dep[x]>=dep[y])
		{
			q[++qn]=sum[x];
			x=fa[x];
		}
		else
		{
			q[++qn]=sum[y];
			y=fa[y];
		}
	}
}
int main()
{
	int n,m,x,y;
	long long ans;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		adde(x,y);
		adde(y,x);
	}
	ex=ey=0;
	memset(c,0,sizeof(c));
	c[1]=1;
	dep[1]=1;
	fa[1]=0;
	work(1);
	if(ex==0)
	{
		work1(1);
		if(sum[1])
		{
			printf("-1");
			return 0;
		}
		for(int i=1;i<=n;i++)
			ans+=abs(sum[i]);
		printf("%lld",ans);
	}
	else if(c[ex]==c[ey])
	{
		work1(1);
		c[ex]-=sum[1]/2;
		c[ey]-=sum[1]/2;
		ans+=abs(sum[1]/2);
		work1(1);
		if(sum[1])
		{
			printf("-1");
			return 0;
		}
		for(int i=1;i<=n;i++)
			ans+=abs(sum[i]);
		printf("%lld",ans);
	}
	else
	{
		work1(1);
		qn=0;
		q[++qn]=0;
		work2(ex,ey);
		sort(q+1,q+1+qn);
		ans+=abs(q[(qn+1)>>1]);
		c[ex]+=q[(qn+1)>>1];
		c[ey]-=q[(qn+1)>>1];
		work1(1);
		if(sum[1])
		{
			printf("-1");
			return 0;
		}
		for(int i=1;i<=n;i++)
			ans+=abs(sum[i]);
		printf("%lld",ans);
	}
	return 0;
}