// Namori
// * frank_c1
// * 2017 / 12 / 18
 
#include <bits/stdc++.h>
using namespace std;

const int maxn = (int)(1e5) + 5;
vector<int> nxt[maxn];
int fa[maxn], dfn[maxn], siz[maxn], su[maxn];
int ci[maxn], cc[maxn], va[maxn];
int cnt, idx;

inline int iabs(int x) {
	return x >= 0 ? x : -x;
}

void dfs(int x, int f, int v) {
	fa[x] = f; dfn[x] = ++idx; 
	siz[x] = 1; su[x] = v; v ^= 1;
	for (int i = 0; i < (int)nxt[x].size(); ++i)
		if (nxt[x][i] != f) {
			if (!dfn[nxt[x][i]]) {
				dfs(nxt[x][i], x, v);
				su[x] += su[nxt[x][i]];
				siz[x] += siz[nxt[x][i]];
			} else if (dfn[nxt[x][i]] < dfn[x]) {
				int c = x, a = nxt[x][i]; 
				while (c != a) {
					ci[++cnt] = c; cc[c] = 1; c = fa[c];
				} ++cnt;
			}
		}
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		nxt[u].push_back(v); nxt[v].push_back(u);
	}
	dfs(1, 0, 0);
	if (!cnt || !(cnt & 1)) {
		if (2 * su[1] != siz[1]) {
			printf("-1\n"); return;
		}
	} else {
		if ((2 * su[1] - siz[1]) & 1) {
			printf("-1\n"); return;
		}
	}
	int dt = (2 * su[1] - siz[1]) / 2, ret = 0;
	for (int i = 1; i <= n; ++i) if (!cc[i]) ret += iabs(2 * su[i] - siz[i]);
	if (cnt) {
		for (int i = 1; i < cnt; ++i) va[i] = 2 * su[ci[i]] - siz[ci[i]];
		va[cnt] = 0; sort(va + 1, va + cnt + 1); ret += iabs(va[(cnt + 1) / 2]) + iabs(dt);
		for (int i = 1; i < cnt; ++i) ret += iabs(va[(cnt + 1) / 2] - dt - 2 * su[ci[i]] + siz[ci[i]]);
	}
	printf("%d\n", ret);
	return 0;
}