#include <algorithm>
#include <cstring>
#include <cstdio>
#define my_abs(x) ((x) < 0 ? -(x) : (x))
int lst[100005], to[200005], pre[200005], seq[100005], tot;
int fa[100005], sum[100005], col[100005], coef[100005];
inline void add_edge(int u, int v)
{
	to[tot] = v;
	pre[tot] = lst[u];
	lst[u] = tot++;
}
void dfs(int u, int cur, int f = -1)
{
	fa[u] = f;
	sum[u] = cur;
	col[u] = cur;
	for (int i = lst[u]; ~i; i = pre[i])
	{
		if (to[i] == f)
			continue;
		dfs(to[i], -cur, u);
		sum[u] += sum[to[i]];
	}
}
int getfa(int u)
{
	if (u == fa[u])
		return u;
	return fa[u] = getfa(fa[u]);
}
int main()
try
{
	// freopen("AGC004-F.in", "r", stdin);
	memset(lst, -1, sizeof(lst));
	int n, m, x, y;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
		fa[i] = i;
	for (int i = 0; i < m; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		if (getfa(--u) == getfa(--v))
		{
			x = u;
			y = v;
			continue;
		}
		fa[getfa(u)] = getfa(v);
		add_edge(u, v);
		add_edge(v, u);
	}
	dfs(0, 1);
	long long ans = 0;
	if (m == n - 1)
	{
		if (sum[0])
			throw false;
	}
	else if (col[x] == col[y])
	{
		if (sum[0] & 1)
			throw false;
		int val = sum[0] >> 1;
		ans += my_abs(val);
		for (int i = x; ~i; i = fa[i])
			sum[i] -= val;
		for (int i = y; ~i; i = fa[i])
			sum[i] -= val;
	}
	else
	{
		if (sum[0])
			throw false;
		for (int i = x; ~i; i = fa[i])
			coef[i]++;
		for (int i = y; ~i; i = fa[i])
			coef[i]--;
		int cnt = 0;
		for (int i = 0; i < n; i++)
		{
			if (coef[i])
				seq[cnt++] = -coef[i] * sum[i];
		}
		std::sort(seq, seq + cnt);
		int val = seq[cnt >> 1];
		ans += my_abs(val);
		for (int i = 0; i < n; i++)
			sum[i] += coef[i] * val;
	}
	for (int i = 0; i < n; i++)
		ans += my_abs(sum[i]);
	printf("%lld\n", ans);
	return 0;
}
catch (bool)
{
	puts("-1");
	return 0;
}
