#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, vis[N], sum[N], dep[N], fa[N], lab[N], cyc_u, cyc_v;
vector<int> G[N];
void dfs(int u, int lst, int depth) {
	fa[u] = lst;
	vis[u] = 1;
	dep[u] = depth;
	sum[u] = ((depth % 2 == 0) ? 1 : -1);
	for (int v : G[u]) if (v != lst) {
		if (!vis[v]) {
			dfs(v, u, depth + 1);
			sum[u] += sum[v];
		} else {
			cyc_u = u;
			cyc_v = v;
		}
	}
}
inline void solve_tree() {
	if (sum[1]) {
		puts("-1");
		return;
	}
	long long ans = 0;
	for (int i = 1; i <= n; i++) ans += abs(sum[i]);
	printf("%lld\n", ans);
}
inline void solve_odd() {
	if (sum[1] % 2) {
		puts("-1");
		return;
	}
	int delta = -sum[1] / 2;
	for (int u = cyc_u; u; u = fa[u]) sum[u] += delta;
	for (int u = cyc_v; u; u = fa[u]) sum[u] += delta;
	long long ans = abs(delta);
	for (int i = 1; i <= n; i++) ans += abs(sum[i]);
	printf("%lld\n", ans);
}
inline void solve_even() {
	if (sum[1]) {
		puts("-1");
		return;
	}
	vector<int> vec;
	vec.push_back(0);
	for (int u = cyc_v; u != cyc_u; u = fa[u]) {
		lab[u] = 1;
		vec.push_back(sum[u]);
	}
	sort(vec.begin(), vec.end());
	int mid = vec[vec.size() / 2];
	long long ans = 0;
	for (int i = 1; i <= n; i++) if (!lab[i]) ans += abs(sum[i]);
	for (int i = 0; i < (int)vec.size(); i++) ans += abs(vec[i] - mid);
	printf("%lld\n", ans);
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(1, 0, 0);
	if (m == n - 1) {
		solve_tree();
	} else if ((dep[cyc_v] - dep[cyc_u]) % 2 == 0) {
		solve_odd();
	} else {
		solve_even();
	}
	return 0;
}