#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

int n, m, coin;
vector<int> adj[100005];

bool odd = false;
bool vis[100005];
int k[100005], a[100005], col[100005]; //y = kx + a

int dn, up; // circle
LL ans = 0LL;

void dfs(int x, int pre = 0) {
	vis[x] = 1;
	for (int i = 0; i < adj[x].size(); ++i) {
		int y = adj[x][i];
		if (y == pre)
			continue;
		if (!vis[y]) {
			col[y] = col[x] ^ 1;
			dfs(y, x);
		} else {
			dn = x, up = y;
			if (col[x] == col[y])
				odd = true;
		}
	}
}

int top;
int stk[100005];

void get(int x, int pre = 0) {
	for (int i = 0; i < adj[x].size(); ++i) {
		int y = adj[x][i];
		if (y != pre && !(x == up && y == dn || x == dn && y == up)) {
			get(y, x);
			k[x] += k[y];
			a[x] += a[y];
		}
	}
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; ++i) {
		int x, y;
		scanf("%d%d", &x, &y);
		adj[x].push_back(y);
		adj[y].push_back(x);
	}
	col[1] = 1;
	dfs(1);
	for (int i = 1; i <= n; ++i)
		a[i] = col[i] ? 1 : -1;
	for (int i = 1; i <= n; ++i)
		coin += a[i];
	if (m == n - 1) {
		if (coin)
			return 0 * puts("-1");
	} else {
		if (odd) {
			if (coin & 1)
				return 0 * puts("-1");
			a[up] -= coin / 2;
			a[dn] -= coin / 2;
			ans += abs(coin / 2);
		} else {
			if (coin)
				return 0 * puts("-1");
			k[up] = -1;
			k[dn] = 1;
		}
	}
	get(1);
	for (int i = 1; i <= n; ++i)
		if (!k[i])
			ans += abs(a[i]);
		else {
			if (k[i]) a[i] = -a[i];
			stk[++top] = a[i];
		}
	stk[++top] = 0;
	sort(stk + 1, stk + top + 1);
	int x = stk[(top + 1) / 2];
	for (int i = 1; i <= top; ++i)
		ans += abs(x - stk[i]);
	printf("%lld\n", ans);
}