#include<bits/stdc++.h>
#define rep(i, a, b) for (register int i=(a); i<=(b); ++i)
#define per(i, a, b) for (register int i=(a); i>=(b); --i)
using namespace std;
const int N=100005;
vector<int> G[N];
int sum[N], fa[N], ex=1, ey=1, ans;

inline int read()
{
 	int x=0,f=1;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}

int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}

void dfs(int u, int f, int val)
{
	sum[u]=val; fa[u]=f;
	for (int v: G[u]) if (v^f)
		dfs(v, u, -val), sum[u]+=sum[v];
}

int main()
{
	int n=read(), m=read();
	rep(i, 1, n) fa[i]=i;
	rep(i, 1, m)
	{
		int x=read(), y=read(), fx=find(x), fy=find(y);
		if (fx^fy) fa[fx]=fy; else {ex=x, ey=y; continue;}
		G[x].push_back(y); G[y].push_back(x);
	}
	dfs(ex, 0, 1);
	if (n==m+1) if (sum[ex]) return puts("-1"), 0;
	if (n==m)
	{
		vector<int> cir;
		for (int i=ey; i; i=fa[i]) cir.push_back(sum[i]);
		if (!(cir.size()&1))
		{
			if (sum[ex]) return puts("-1"), 0;
			sort(cir.begin(), cir.end());
			for (int i=ey; i; i=fa[i]) sum[i]-=cir[cir.size()>>1];
		}
		else
		{
			if (sum[ex]&1) return puts("-1"), 0;
			for (int i=ey; i; i=fa[i]) sum[i]-=sum[ex]>>1;
		}
	}
	rep(i, 1, n) ans+=abs(sum[i]);
	printf("%d\n", ans);
	return 0;
}
