#include <bits/stdc++.h>

const int MAXN = 100010;
typedef long long LL;
void bye() { std::cout << -1 << std::endl; exit(0); }
int head[MAXN], nxt[MAXN << 1], to[MAXN << 1], tot;
void addedge(int b, int e) {
	nxt[++tot] = head[b]; to[head[b] = tot] = e;
	nxt[++tot] = head[e]; to[head[e] = tot] = b;
}
template<class T> T absx(T x) { return x < 0 ? -x : x; }
int n, m; LL ans;
int ins[MAXN], st[MAXN], top, L;
int dfs(int u, int fa = 0) {
	int res = 1;
	for (int i = head[u]; i; i = nxt[i])
		if (to[i] != fa && !ins[to[i]])
			res -= dfs(to[i], u);
	if (fa) ans += absx(res);
	return res;
}
int findcir(int u, int fa = 0) {
	if (ins[u]) {
		int t; L = top;
		do t = st[top--]; while (t != u);
		return true;
	}
	ins[u] = true; st[++top] = u;
	for (int i = head[u]; i; i = nxt[i])
		if (to[i] != fa)
			if (findcir(to[i], u))
				return true;
	ins[u] = false; --top;
	return false;
}
int tl[MAXN];
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	std::cin >> n >> m;
	for (int i = 1, t1, t2; i <= m; ++i) {
		std::cin >> t1 >> t2;
		addedge(t1, t2);
	}
	if (m == n - 1) {
		if (dfs(1)) bye();
		std::cout << ans << std::endl;
		return 0;
	}
	findcir(1);
	for (int i = 1; i <= top; ++i) ins[st[i]] = false;
	for (int i = top + 1; i <= L; ++i)
		tl[i - top] = dfs(st[i]);
	L -= top;
	for (int i = 1; i <= L; ++i) tl[i] -= tl[i - 1];
	int v = tl[L] >> 1;
	for (int i = 1; i <= L; ++i) if (i & 1 ^ 1) tl[i] = -tl[i];
	if (L & 1) { if (tl[L] & 1) bye(); }
	else if (tl[L]) bye();
	if (~L & 1) std::nth_element(tl + 1, tl + L / 2, tl + 1 + L), v = tl[L + 1 >> 1];
	for (int i = 1; i <= L; ++i) ans += absx(v - tl[i]);
	std::cout << ans << std::endl;
	return 0;
}
