#include<bits/stdc++.h>
using namespace std;

const int MAXN = 100010;

template<typename T> inline T read() {
	T x(0), f(1);
	char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch=='-') f = -1;
	for(; isdigit(ch); ch = getchar()) x = (x*10)+(ch^48);
	return x * f;
}

int n, m;
int e, st[MAXN], to[MAXN<<1];
int nxt[MAXN<<1];
int dep[MAXN], A, B;
bool vis[MAXN], cycle;
inline void Add(int u, int v) {
	to[++e] = v, nxt[e] = st[u];
	st[u] = e;
}

namespace Tree {
	int ans, ans1, ans2, size[MAXN];
	int dfs(int u, int fa, int dep) {
		int res = dep&1 ? 1 : 0;
		if(dep&1) ans1++;
		else ans2++;
		size[u] = 1;
		for(int i = st[u]; i; i = nxt[i]) {
			if(to[i] == fa) continue;
			res += dfs(to[i], u, dep+1);
			size[u] += size[to[i]];
		}
		if(u != 1) ans += abs(size[u]-(res<<1));
		return res;
	}
	
	inline void solve() {
		for(int i = 1; i < n; i++) {
			int u = read<int>(), v = read<int>();
			Add(u, v), Add(v, u);
		}
		dfs(1, 1, 1);
		if(ans1 == ans2) printf("%d\n", ans);
		else printf("-1\n");
	}
}

namespace Odd {
	int n1, n2, val[MAXN];
	int size[MAXN], ans;
	bool L;
	int dfs(int u, int fa) {
		//printf("dfs(%d, %d){\n", u, fa);
		int res = val[u];
		size[u] = (dep[u]&1)!=L;
		for(int i = st[u]; i; i = nxt[i]) {
			int v = to[i];
			if(v == fa) continue;
			if(v == A && u == B) continue;
			if(v == B && u == A) continue;
			res += dfs(v, u);
			size[u] += size[v];
		}
		if(fa) ans += abs(size[u]-res);
		//printf("%d\n", abs(size[u]-res));
		//printf("}\n");
		return res;
	}
	inline void solve() {
		L = dep[A]&1;
		for(int i = 1; i <= n; i++)
			if((dep[i]&1) == L) {
				val[i]++;
				n1++;
			}
			else n2++;
		if((abs(n2-n1))&1) {
			printf("-1\n");
			return;
		}
		/*printf("%d %d\n", A, B);
		printf("%d %d\n", n1, n2);*/
		val[A] += (n2-n1)>>1, val[B] += (n2-n1)>>1;
		dfs(1, 0);
		printf("%d\n", ans+((abs(n2-n1))>>1));
	}
}

namespace Even {
	int s[MAXN], ans;
	int C[MAXN], cnt, t[MAXN];
	int size[MAXN], a[MAXN];
	bool onc[MAXN];
	bool findcycle(int u, int f) {
		if(u == B) return true;
		for(int i = st[u]; i; i = nxt[i]) {
			int v = to[i];
			if(v == f) continue;
			if(u == A && v == B) continue;
			C[++cnt] = v;
			if(findcycle(v, u)) return true;
			cnt--;
		}
		return false;
	}
	int dfs(int u, int f, bool d) {
		int res = d&1;
		size[u] = 1;
		for(int i = st[u]; i; i = nxt[i]) {
			int v = to[i];
			if(onc[v]) continue;
			if(v == f) continue;
			res += dfs(v, u, d^1);
			size[u] += size[v];
		}
		if(f) ans += abs(size[u]-(res<<1));
		return res;
	}
	inline void solve() {
		C[cnt = 1] = A;
		//printf("%d %d\n", A, B);
		findcycle(A, 0);
		for(int i = 1; i <= cnt; i++) onc[C[i]] = true;
		for(int i = 1; i <= cnt; i++) 
			s[i] = dfs(C[i], 0, i&1), t[i] = size[C[i]]-s[i];
		for(int i = 1; i < cnt; i++) {
			s[i] += s[i-1], t[i] += t[i-1];
			a[i] = s[i]-t[i];
		}
		s[cnt] += s[cnt-1], t[cnt] += t[cnt-1];
		if(s[cnt] != t[cnt]) {
			printf("-1\n");
			return;
		}
		a[cnt] = 0;
		sort(a+1, a+cnt+1);
		for(int i = 1; i <= cnt; i++) 
			ans += abs(a[(cnt+1)>>1]-a[i]);
		printf("%d\n", ans);
	}
}

void dfs(int u, int fa) {
	vis[u] = true;
	for(int i = st[u]; i; i = nxt[i]) {
		int v = to[i];
		if(v == fa) continue;
		if(vis[v]) {
			A = u, B = v;
			if((dep[v]&1)==(dep[u]&1)) cycle = true;
			continue;
		}
		dep[v] = dep[u]+1, dfs(v, u);
	}
}

int main() {
	n = read<int>(), m = read<int>();
	if(m == n-1) Tree::solve();
	else {
		for(int i = 1; i <= m; i++) {
			int u = read<int>(), v = read<int>();
			Add(u, v), Add(v, u);
		}
		dfs(dep[1] = 1, 0);
		if(cycle) Odd::solve();
		else Even::solve();
	}
	return 0;
}



Measured memory_bytes: 10624