#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
template<typename T> inline void chkmin(T &a, const T &b) { a = a < b ? a : b; }
template<typename T> inline void chkmax(T &a, const T &b) { a = a > b ? a : b; }

const int MAXN = 100005;
int vis[MAXN], sz[MAXN], dep[MAXN], val[MAXN], n, m, s, t;
vector<int> gra[MAXN];
LL ans;

void dfs(int u, int fa) {
	vis[u] = 1;
	for (int v : gra[u]) if (v != fa) {
		if (vis[v]) { s = u, t = v; continue; }
		dep[v] = dep[u] ^ 1;
		dfs(v, u);
	}
}

void dfs1(int u, int fa) {
	for (int v : gra[u]) if (v != fa) {
		if ((s == u && t == v) || (s == v && t == u)) continue;
		dfs1(v, u);
		sz[u] += sz[v], val[u] += val[v];
	}
	if (!val[u]) ans += abs(sz[u]);
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++) {
		int u, v; scanf("%d%d", &u, &v);
		gra[u].push_back(v);
		gra[v].push_back(u);
	}
	dfs(1, 0);
	int d = 0, odd = dep[s] == dep[t];
	for (int i = 1; i <= n; i++)
		d += (sz[i] = dep[i] ? -1 : 1);
	if (m == n && odd) {
		if (d & 1) return puts("-1"), 0;
		sz[s] -= d / 2, sz[t] -= d / 2;
		ans += abs(d / 2);
	} else if (m == n && !odd) {
		if (d) return puts("-1"), 0;
		val[s] = 1, val[t] = -1;
	} else if (d) return puts("-1"), 0;
	
	dfs1(1, 0);
	if ((m == n && odd) || m < n)
		return printf("%lld\n", ans), 0;
	int cnt = 0;
	for (int i = 1; i <= n; i++) if (val[i])
		vis[++cnt] = -val[i] * sz[i];
	vis[++cnt] = 0;
	nth_element(vis + 1, vis + 1 + cnt / 2, vis + cnt + 1);
	int x = vis[1 + cnt / 2];
	for (int i = 1; i <= cnt; i++)
		ans += abs(vis[i] - x);
	printf("%lld\n", ans);
	return 0;
}