#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
namespace Dango
{
	const int MAXN=100005;
	vector<int> cir;
	int n,m;
	int head[MAXN],to[MAXN<<1],nxt[MAXN<<1];
	int fa[MAXN];
	int d[MAXN],tfa[MAXN];
	int rt,ed;
	int res[MAXN];
	long long ans;
	int read()
	{
		int x=0,f=0;
		char ch=getchar();
		while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
		while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
		return f?-x:x;
	}
	int ffind(int a){return fa[a]==a?a:fa[a]=ffind(fa[a]);}
	void add(int u,int v)
	{
		static int cnt;
		cnt++;
		to[cnt]=v;
		nxt[cnt]=head[u];
		head[u]=cnt;
	}
	void dfs(int u,int f)
	{
		d[u]=d[f]+1;
		tfa[u]=f;
		res[u]+=d[u]&1?1:-1;
		for(int i=head[u];i;i=nxt[i])
		{
			int v=to[i];
			if(v==f)continue;
			dfs(v,u);
			res[u]+=res[v];
		}
	}
	void solve_tree()
	{
		dfs(1,0);
		if(res[1]!=0)ans=-1;
		else
			for(int i=1;i<=n;i++)
				ans+=res[i]<0?-res[i]:res[i];
		printf("%lld",ans);
	}
	void solve()
	{
		dfs(rt,0);
		for(int i=ed;i;i=tfa[i])cir.push_back(res[i]);
		if(cir.size()&1)
		{
			if(res[rt]&1)ans=-1;
			else
			{
				for(int i=ed;i;i=tfa[i])
					res[i]-=res[rt]>>1;
				for(int i=1;i<=n;i++)
					ans+=res[i]<0?-res[i]:res[i];
			}
		}
		else
		{
			if(res[rt])ans=-1;
			else
			{
				sort(cir.begin(),cir.end());
				int mid=cir.size()>>1;
				for(int i=ed;i;i=tfa[i])
					res[i]-=cir[mid];
				for(int i=1;i<=n;i++)
					ans+=res[i]<0?-res[i]:res[i];
			}
		}
		printf("%lld",ans);
	}
	int work()
	{
		n=read();m=read();
		for(int i=1;i<=n;i++)
			fa[i]=i;
		for(int i=1;i<=m;i++)
		{
			int u=read(),v=read();
			int a=ffind(u),b=ffind(v);
			if(a!=b)
			{
				add(u,v);add(v,u);
				fa[a]=b;
			}
			else{rt=u;ed=v;}
		}
		if(m==n-1)solve_tree();
		else solve();
		return 0;
	}
}
int main()
{
	return Dango::work();
}