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

typedef long long ll;

const int N = 100005;
int n, m, a, b, fa[N], size[N], dep[N];
vector<int> g[N];

void dfs(int u) {
	size[u] = dep[u] & 1 ? 1 : -1;
	for (int v : g[u]) {
		if (v == fa[u]) continue;
		if (dep[v] == -1) {
			fa[v] = u;
			dep[v] = dep[u] + 1;
			dfs(v);
			size[u] += size[v];
		} else if (dep[v] < dep[u])
			a = v, b = u;
	}
}

void solve() {
	scanf("%d%d", &n, &m);
	for (int i = 1, a, b; i <= m; ++i) {
		scanf("%d%d", &a, &b);
		g[a].push_back(b), g[b].push_back(a);
	}
	fill(dep + 2, dep + 1 + n, -1);
	dfs(1);
	if (n - m == 1) {
		if (size[1]) puts("-1");
		else {
			ll ans = 0;
			for (int i = 2; i <= n; ++i) ans += abs(size[i]);
			printf("%lld\n", ans);
		}
		return;
	}
	if (dep[a] % 2 == dep[b] % 2) {
		if (size[1] % 2) puts("-1");
		else {
			ll d = -size[1] / 2;
			for (int i = b; i != a; i = fa[i]) size[i] += d;
			for (int i = a; i; i = fa[i]) size[i] += 2 * d;
			ll ans = abs(d);
			for (int i = 2; i <= n; ++i) ans += abs(size[i]);
			printf("%lld\n", ans);
		}
		return;
	}
	if (size[1]) puts("-1");
	else {
		vector<int> tmp{0};
		for (int i = b; i != a; i = fa[i]) tmp.push_back(size[i]);
		nth_element(tmp.begin(), tmp.begin() + tmp.size() / 2, tmp.end());
		ll ans = 0;
		for (int x : tmp) ans += abs(x - tmp[tmp.size() / 2]) - abs(x);
		for (int i = 2; i <= n; ++i) ans += abs(size[i]);
		printf("%lld\n", ans);
	}
}

int main() {
	solve();
}
