#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;

typedef long long LL;
const int N = 1e5 + 10;
struct E {int v, nt;} e[N<<1];
int n, m, su, sv, cri, tot, h[N], s[N];
int f[N], g[N], A[N], dep[N], vis[N]; LL ans;

inline int abs(int x) {return x < 0 ? -x : x;}
inline void add(int u, int v) {
	e[++tot] = (E){v, h[u]}, h[u] = tot;
	e[++tot] = (E){u, h[v]}, h[v] = tot;
}
template <class T> inline void in(T &x) {
	x = 0; int f = 1; char ch = getchar();
	for (; ch<'0' || ch>'9';) {if (ch=='-') f=-1; ch = getchar();}
	for (; ch>='0' && ch<='9';) x = x*10 + ch-'0', ch = getchar();
	x *= f;
}

inline void dfs(int x, int p) {
	for (int v, i = h[x]; i; i = e[i].nt) {
		if ((v = e[i].v) == p) continue;
		if (!f[v]) f[v] = -f[x], dfs(v, x);
		else su = x, sv = v, cri = (f[v] == f[x]);
	}
}

inline void dfs2(int x, int p) {
	s[x] = f[x];
	for (int v, i = h[x]; i; i = e[i].nt) {
		v = e[i].v;
		if (v == p || (x==su&&v==sv) || (x==sv&&v==su)) continue;
		dfs2(v, x), s[x] += s[v], g[x] += g[v];
	}
}

int main() {
	int u, v; in(n), in(m);
	for (int i = 1; i <= m; ++i) in(u), in(v), add(u, v);
	f[1] = 1, dfs(1, 0);
	if (m == n - 1) {
		dfs2(1, 0);
		if (s[1]) {puts("-1"); return 0;}
		for (int i = 1; i <= n; ++i) ans += abs(s[i]);
	} else if (cri & 1) {
		dfs2(1, 0);
		if (s[1]%2) {puts("-1"); return 0;}
		int tmp = s[1];
		f[su] -= tmp >> 1, f[sv] -= tmp >> 1, ans = abs(tmp >> 1);
		dfs2(1, 0);
		for (int i = 1; i <= n; ++i) ans += (LL)abs(s[i]);
	} else {
		++g[su], --g[sv];
		dfs2(1, 0);
		if (s[1]) {puts("-1"); return 0;}
		int tmp, num = 0;
		for (int i = 1; i <= n; ++i)
			if (!g[i]) ans += (LL)abs(s[i]);
			else if (g[i]) A[++num] = s[i];
			else A[++num] = -s[i];
		A[++num] = 0;
		sort(A + 1, A + num + 1);
		tmp = A[num + 1 >> 1];
		for (int i = 1; i <= num; ++i) ans += (LL)abs(tmp - A[i]);
	}
	printf("%lld\n", ans);
	return 0;
}