#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 100005;

template <typename T> inline void read(T &AKNOI) {
	T x = 0, flag = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') flag = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	AKNOI = flag * x;
}

inline void fail() {
	printf("-1\n");
	exit(0);
}

int n, m;
vector<int> g[MAXN];
int fa[MAXN], dep[MAXN], siz[MAXN], cu, cv, stk[MAXN], top;

void DFS(int u, int ff) {
	siz[u] = (dep[u] & 1? 1 : -1);
	for (auto v : g[u]) {
		if (v == ff) continue;
		if (v == 1 || fa[v]) {
			cu = u; cv = v;
		} else {
			fa[v] = u;
			dep[v] = dep[u] + 1;
			DFS(v, u);
			siz[u] += siz[v];
		}
	}
}

void init() {
	read(n); read(m);
	for (int i = 1, u, v; i <= m; ++i) {
		read(u); read(v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
}

void solve() {
	LL ans = 0;
	DFS(1, 0);
	if (m == n - 1) {
		if (siz[1]) fail();
	} else if ((dep[cu] - dep[cv]) & 1) { //even circle
		if (siz[1]) fail();
		for (int u = cv; u != cu; u = fa[u]) {
			stk[++top] = siz[u];
		}
		++top;
		sort(stk + 1, stk + top + 1);
		int mid = top / 2;
		for (int i = 1; i <= top; ++i) {
			ans += abs(stk[i] - stk[mid]) - abs(stk[i]);
		}
	} else { //odd circle
		if (siz[1] & 1) fail();
		int d = -siz[1] / 2, u;
		ans += abs(d);
		for (u = cv; u != cu; u = fa[u]) {
			siz[u] += d;
		}
		for (; u; u = fa[u]) {
			siz[u] += d * 2;
		}
	}
	for (int i = 1; i <= n; ++i) {
		ans += abs(siz[i]);
	}
	printf("%lld\n", ans);
}

int main() {
	init();
	solve();
	return 0;
}
