#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=100005;
struct node { int y,next; } a[N<<1];
LL an;
int S[N],fa[N],h[N],head[N],s[N],k[N],i,j,n,m,x,y,z,g;
char c;

inline char getc()
{
	#define VV 10000000
	static char s[VV],*l=s,*r=s;
	if (l==r)
		l=s,r=s+fread(s,1,VV,stdin);
	return l==r?0:*l++;
}

int read(){ int z=0; do c=getc(); while (c<'0'||c>'9'); while (c>='0'&&c<='9') z*=10,z+=c-'0',c=getc(); return z; }

void jb(int x,int y)
{
	a[++g]=(node){y,head[x]},head[x]=g,
	a[++g]=(node){x,head[y]},head[y]=g;
}

void dfs(int w,int sp)
{
	h[w]=h[fa[w]]+1,
	s[w]=h[w]&1?1:-1;
	for (register int j=head[w];j;j=a[j].next)
		if ((j^1)!=sp)
		{
			int c=a[j].y;
			if (h[c])
				x=w,y=c;
			else
				fa[c]=w,dfs(c,j),s[w]+=s[c];
		}
}

int main()
{
	n=read(),m=read(),g=1;
	for (i=1;i<=m;++i) jb(read(),read());
	dfs(1,0);
	if (m==n-1)
	{
		if (s[1])
			an=-1;
		else
			for (i=2;i<=n;++i)
				an+=abs(s[i]);
	}
	else
		if (h[x]-h[y]&1)
			if (s[1])
				an=-1;
			else
			{
				S[g=1]=0;
				for (;x;x=fa[x])
					++k[x];
				for (;y;y=fa[y])
					--k[y];
				for (i=1;i<=n;++i)
					if (k[i])
						S[++g]=k[i]*s[i];
					else
						an+=abs(s[i]);
				sort(S+1,S+g+1);
				z=S[g+1>>1];
				for (i=1;i<=g;++i)
					an+=abs(z-S[i]);
			}
		else
		{
			
			if (abs(s[1])&1)
				an=-1;
			else
			{
				an=abs(z=s[1]/2);
				for (;x;x=fa[x])
					s[x]-=z;
				for (;y;y=fa[y])
					s[y]-=z;
				for (i=2;i<=n;++i)
					an+=abs(s[i]);
			}
		}
	printf("%lld\n",an);
	return 0;
}