#include<bits/stdc++.h>
using namespace std;
vector <int> des[1000010];
int col[1000010],sum[1000010],fa[1000010],st[1000010],ff[1000010];
bool ins[1000010];
int getf(int s){if (ff[s]==s) return s;return ff[s]=getf(ff[s]);}
void dfs(int s,int pre)
{
	col[s]=-col[pre];sum[s]=col[s];fa[s]=pre;
	for (int k=0;k<des[s].size();k++) if (des[s][k]!=pre)
	{
		dfs(des[s][k],s);
		sum[s]+=sum[des[s][k]];
	}
}
int main()
{
	int n,m,x=1,y=0;scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) ff[i]=i;
	for (int i=1;i<=m;i++)
	{
		int u,v;scanf("%d%d",&u,&v);
		if (getf(u)==getf(v)) {x=u;y=v;continue;}
		ff[getf(u)]=getf(v);
		des[u].push_back(v);des[v].push_back(u);
	}
	col[0]=-1;dfs(x,0);
	if (!y)
	{
		if (sum[x]) {printf("-1\n");return 0;}
		long long ans=0;
		for (int i=1;i<=n;i++) ans+=abs(sum[i]);
		printf("%lld\n",ans);return 0;
	}
	int top=0;
	for (int i=y;i!=0;i=fa[i]) {ins[i]=true;st[++top]=sum[i];}if (st[top]!=sum[x]) assert(0);
	if (top&1)
	{
		if (sum[x]&1) {printf("-1\n");return 0;}	
		long long ans=0;
		for (int i=1;i<=n;i++) if (ins[i]) ans+=abs(sum[i]-sum[x]/2);else ans+=abs(sum[i]);
		printf("%lld\n",ans);return 0;
	}
	if (sum[x]) {printf("-1\n");return 0;}
	sort(st+1,st+top+1);
	long long ans=0;
	for (int i=1;i<=n;i++) if (ins[i]) ans+=abs(sum[i]-st[top/2]);else ans+=abs(sum[i]);
	printf("%lld\n",ans);
}