#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 100100
inline int read()
{
	int x=0;bool t=false;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')t=true,ch=getchar();
	while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
	return t?-x:x;
}
struct Line{int v,next;}e[MAX<<1];
int h[MAX],cnt=1;
inline void Add(int u,int v){e[cnt]=(Line){v,h[u]};h[u]=cnt++;}
int n,m,dep[MAX],s[MAX],k[MAX],ln,U,V,S[MAX],top,tot;
ll ans;
void dfs(int u,int ff)
{
	dep[u]=dep[ff]+1;s[u]=(dep[u]&1)?1:-1;tot+=s[u];
	for(int i=h[u];i;i=e[i].next)
	{
		int v=e[i].v;if(v==ff)continue;
		if(dep[v])ln=abs(dep[u]-dep[v])+1,U=u,V=v;
		else dfs(v,u);
	}
}
void dfs2(int u,int ff)
{
	for(int i=h[u];i;i=e[i].next)
	{
		int v=e[i].v;
		if(v==ff||(U==u&&V==v)||(U==v&&V==u))continue;
		dfs2(v,u),k[u]+=k[v],s[u]+=s[v];
	}
	if(k[u]==0)ans+=abs(s[u]);
	else S[++top]=k[u]*s[u];
}
int main()
{
	n=read();m=read();
	for(int i=1;i<=m;++i)
	{
		int a=read(),b=read();
		Add(a,b);Add(b,a);
	}
	dfs(1,0);
	if(m==n-1){if(tot!=0){puts("-1");return 0;}}
	else if(ln&1)
	{
		if(tot%2==1){puts("-1");return 0;}
		ans+=abs(tot/2);s[U]-=tot/2,s[V]-=tot/2;
	}
	else
	{
		if(tot!=0){puts("-1");return 0;}
		k[U]=1;k[V]=-1;
	}
	dfs2(1,0);
	S[++top]=0;sort(&S[1],&S[top+1]);
	for(int i=1;i<=top;++i)ans+=abs(S[(top+1)/2]-S[i]);
	cout<<ans<<endl;
	return 0;
}
