#include <cstdio>
#include <algorithm>
#include <vector>

typedef long long LL;
const int MN = 100005;

int N, M;
std::vector<int> G[MN];

int a, b, vis[MN], par[MN], dep[MN], num1[MN], num2[MN], kval[MN];
void DFS(int u, int fr) {
	vis[u] = 1;
	dep[u] = dep[par[u] = fr] + 1;
	num1[u] = dep[u] & 1;
	num2[u] = ~dep[u] & 1;
	for (int v : G[u]) if (v != fr) {
		if (!vis[v]) DFS(v, u), num1[u] += num1[v], num2[u] += num2[v];
		else a = u, b = v;
	}
}

int main() {
	scanf("%d%d", &N, &M);
	if (N & 1) return puts("-1"), 0;
	for (int i = 1; i <= M; ++i) {
		int x, y;
		scanf("%d%d", &x, &y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	DFS(1, 0);
	if (M == N - 1) {
		if (num1[1] != num2[1]) return puts("-1"), 0;
		LL Ans = 0;
		for (int i = 2; i <= N; ++i) {
			int x = num2[i] - num1[i];
			Ans += x < 0 ? -x : x;
		}
		printf("%lld\n", Ans);
	} else {
		if ((dep[a] ^ dep[b]) & 1) {
			if (num1[1] != num2[1]) return puts("-1"), 0;
			for (int x = a; x; x = par[x]) ++kval[x];
			for (int x = b; x; x = par[x]) --kval[x];
			LL Ans = 0;
			static int seq[MN], cnt;
			seq[cnt = 1] = 0;
			for (int i = 2; i <= N; ++i) {
				if (!kval[i]) {
					int x = num2[i] - num1[i];
					Ans += x < 0 ? -x : x;
				} else
					seq[++cnt] = kval[i] * (num2[i] - num1[i]);
			}
			std::sort(seq + 1, seq + cnt + 1);
			int mid = seq[cnt / 2 + 1];
			for (int i = 1; i <= cnt; ++i)
				Ans += seq[i] < mid ? mid - seq[i] : seq[i] - mid;
			printf("%lld\n", Ans);
		} else {
			int k = N / 2 - num1[1];
			for (int x = a; x; x = par[x]) num1[x] += k;
			for (int x = b; x; x = par[x]) num1[x] += k;
			LL Ans = 0;
			for (int i = 2; i <= N; ++i) {
				int x = num2[i] - num1[i];
				Ans += x < 0 ? -x : x;
			}
			printf("%lld\n", Ans + (k < 0 ? -k : k));
		}
	}
	return 0;
}