#include <bits/stdc++.h>
using namespace std;

typedef long long lint;
const int maxn = 100005;

int n, m;
int fa[maxn], pu, pv;

int f[maxn], siz[maxn], dep[maxn], col[maxn], cnt, p[maxn];
lint ans;

struct edge
{
	int to, next;
} e[maxn * 2];
int h[maxn], tot;

inline int gi()
{
	char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	int sum = 0;
	while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
	return sum;
}

inline int find(int x)
{
	if (!fa[x]) return x;
	return fa[x] = find(fa[x]);
}

inline void add(int u, int v)
{
	e[++tot] = (edge) {v, h[u]}; h[u] = tot;
	e[++tot] = (edge) {u, h[v]}; h[v] = tot;
}

void dfs(int u, int fa, int t)
{
	siz[u] += t == 0 ? 1 : -1; col[u] = t;
	dep[u] = dep[fa] + 1;
	for (int i = h[u], v; v = e[i].to, i; i = e[i].next)
		if (v != fa) {
			f[v] = u, dfs(v, u, t ^ 1);
			siz[u] += siz[v];
		}
	ans += abs(siz[u]);
}

namespace case1
{
	
	void solve()
	{
		if (siz[1] == 0) printf("%lld\n", ans);
		else puts("-1");
	}

}

namespace case2
{
	
	void solve()
	{
		if (siz[1] & 1) return puts("-1"), void();
		int cnt = siz[1] >> 1;
		memset(siz + 1, 0, sizeof(int) * n);
		ans = abs(cnt); siz[pu] -= cnt; siz[pv] -= cnt;
		dfs(1, 0, 0);
		printf("%lld\n", ans);
	}
	
}

namespace case3
{

    void solve()
	{
		if (siz[1]) return puts("-1"), void();
		ans = 0;
		while (pu != pv) {
			if (dep[pu] > dep[pv]) p[++cnt] = siz[pu], siz[pu] = 0, pu = f[pu];
			else p[++cnt] = -siz[pv], siz[pv] = 0, pv = f[pv];
		}
		p[++cnt] = 0;
		sort(p + 1, p + cnt + 1);
		for (int i = 1; i <= cnt; ++i)
			ans += abs(p[(cnt + 1) >> 1] - p[i]);
		for (int i = 1; i <= n; ++i) ans += abs(siz[i]);
		printf("%lld\n", ans);
	}
	
}

int main()
{
	n = gi(); m = gi();
	for (int u, v, i = 1; i <= m; ++i) {
		u = gi(); v = gi();
		if (find(u) != find(v)) add(u, v), fa[find(u)] = v;
		else pu = u, pv = v;
	}
	
	dfs(1, 0, 0);
	
	if (m == n - 1) case1::solve();
	else if (col[pu] == col[pv]) case2::solve();
	else case3::solve();
	
	return 0;
}