#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define mp make_pair
#define reg register int
#define pii pair<int,int>
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

struct edge {
	int x, y, next;
} e[N * 2]; int len, last[N];
int sx, sy, dep[N], s[N], val[N];
int tp, sta[N];
bool v[N];

I void ins(cr x, cr y) {e[++len] = edge{x, y, last[x]}, last[x] = len;}

void dfs(cr x, cr fa) {
	v[x] = 1;
	for(reg k = last[x], y = e[k].y; k; k = e[k].next, y = e[k].y) if(y ^ fa){
		if(v[y]) sx = x, sy = y;
		else dep[y] = dep[x] + 1, dfs(y, x);
	}
}
void solve(cr x) {
	v[x] = 1;
	for(reg k = last[x], y = e[k].y; k; k = e[k].next, y = e[k].y) if(!v[y]){
		solve(y);
		val[x] += val[y];
		s[x] += s[y];
	}
}

int main() {
	reg n = read(), m = read();
	for(reg i = 1; i <= m; i++) {
		reg x = read(), y = read();
		ins(x, y), ins(y, x);
	} dfs(1, 0); LL ans = 0;
	reg u = 0; for(reg i = 1; i <= n; i++) {
		if(dep[i] & 1) s[i] = 1, u++;
		else s[i] = -1, u--;
	} if(n == m) {
		if((dep[sx] & 1) ^ (dep[sy] & 1)) {
			if(u) {puts("-1"); return 0;}
			val[sx]++, val[sy]--;
		} else {
			if(u & 1) {puts("-1"); return 0;}
			s[sx] -= u / 2, s[sy] += u / 2;
			ans += u / 2;
		}
	} else if(u) {puts("-1"); return 0;}
	memset(v, 0, sizeof(v));
	solve(1);
	sta[++tp] = 0;
	for(reg i = 1; i <= n; i++) {
		if(!val[i]) ans += abs(s[i]);
		else if(val[i] == -1) sta[++tp] = -s[i];
		else sta[i] = s[i];
	} sort(sta + 1, sta + tp + 1);
	reg c = sta[tp + 1 >> 1];
	for(reg i = 1; i <= tp; i++) ans += abs(sta[i] - c);
	pr2(ans);
	return 0;
}

