#include <bits/stdc++.h>
#define ad(x) ((x - 1 ^ 1) + 1)

typedef long long ll;
const int N = 100054, M = N * 2;

int n, m, E = 0, L;
int to[M], first[N], next[M];
int p[N], dep[N], ssize[N]; // signed size
int cnt = 0, buf[N];

inline void addedge(int u, int v) {
	to[++E] = v, next[E] = first[u], first[u] = E;
	to[++E] = u, next[E] = first[v], first[v] = E;
}

void dfs(int x) {
	int i, y; ssize[x] = -(dep[x] & 1) | 1;
	for (i = first[x]; i; i = next[i])
		if (!~p[y = to[i]])
			p[y] = x, dep[y] = dep[x] + 1, dfs(y), ssize[x] += ssize[y];
		else if (dep[x] < dep[y])
			L = i;
//	fprintf(stderr, "ssize[%d] = %d\n", x, ssize[x]);
}

int main() {
	int i, u, v, incr; ll ans = 0;
	scanf("%d%d", &n, &m);
	for (i = 0; i < m; ++i) scanf("%d%d", &u, &v), addedge(u, v);
	memset(p, -1, sizeof p), p[1] = 0, dfs(1);
	if (m == n - 1) {
		if (ssize[1]) return puts("-1"), 0;
		for (i = 1; i <= n; ++i) ans += abs(ssize[i]);
		return printf("%lld\n", ans), 0;
	}
	u = to[ad(L)], v = to[L];
	assert(m == n && dep[u] < dep[v]);
	fprintf(stderr, "u = %d, v = %d\n", u, v);
	if ((dep[u] ^ dep[v]) & 1) {
		if (ssize[1]) return puts("-1"), 0;
		for (i = v; i != u; i = p[i]) buf[cnt++] = ssize[i];
		++cnt, std::nth_element(buf, buf + cnt / 2, buf + cnt);
		for (i=0;i<cnt;++i)
			fprintf(stderr, "buf[%d] = %d\n", i, buf[i]);
		for (i = 0; i < cnt; ++i) ans += abs(buf[i] - buf[cnt / 2]) - abs(buf[i]);
	} else {
		if (ssize[1] & 1) return puts("-1"), 0;
		incr = -ssize[1] / 2, ans = abs(incr);
		for (i = v; i != u; i = p[i]) ssize[i] += incr;
		for (; i; i = p[i]) ssize[i] += 2 * incr;
	}
	for (i = 1; i <= n; ++i) ans += abs(ssize[i]);
	printf("%lld\n", ans);
	return 0;
}