#include <bits/stdc++.h>
#define int long long
using namespace std;
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
typedef long long LL;
const int maxn = 1e5 + 5;
const LL mod = 1e9 + 7;
const int inf = 50000;

int n, m, val[maxn], d[maxn], cut, x, y, siz, first[maxn], cnt, ans, res, par[maxn];
int f[maxn], tot, a[maxn];
bool vis[maxn];
struct Edge {
	int to, next;
} e[maxn<<1];

void add(int u, int v) {
	e[cnt] = (Edge) {v, first[u]};
	first[u] = cnt++;
}

void Dfs1(int u, int fa) {
	par[u] = fa;
	d[u] = d[fa] + 1;
	val[u] = (d[u]&1) ? 1:-1;
	vis[u] = 1;
	for(int i = first[u]; i != -1; i = e[i].next) {
		int v = e[i].to;
		if(v == fa) continue;
		if(vis[v]) {
			siz = d[u] + d[v] - 1;
			cut = i;
			x = u, y = v;
			continue;
		}
		Dfs1(v, u);
		val[u] += val[v];
	}
}

void Dfs2(int u, int fa) {
	for(int i = first[u]; i != -1; i = e[i].next) {
		int v = e[i].to;
		if(v == fa || i == cut || (i^1) == cut) continue;
		Dfs2(v, u);
		f[u] += f[v];
	}
}

signed main() {
	cin>>n>>m;
	memset(first, -1, sizeof(first));
	rep(i, 1, m) {
		int u, v;
		cin>>u>>v;
		add(u, v); 
		add(v, u);
	}
	Dfs1(1, 0);
	if(m == n - 1) {
		if(val[1]) cout<<-1<<endl;
		else {
			rep(i, 1, n)
			    ans += abs(val[i]);
			cout<<ans<<endl;
		}
	}
	if(m == n) {
		if(siz&1) {
			if(val[1]%2 != 0) cout<<-1<<endl;
			else {
				res = -val[1]/2;
				while(x) {
					val[x] += res;
					x = par[x];
				}
				while(y) {
					val[y] += res;
					y = par[y];
				}
				rep(i, 1, n)
			    	ans += abs(val[i]);
				cout<<ans + abs(res)<<endl;
			}
		}
		else {
			if(val[1]) cout<<-1<<endl;
			else {
				a[++tot] = 0;
				f[x] = 1, f[y] = -1;
				Dfs2(1, 0);
				rep(i, 1, n) {
				    if(!f[i]) ans += abs(val[i]);
				    else a[++tot] = -f[i]*val[i];
				}
				sort(a + 1, a + tot + 1);
				int vd = a[(tot + 1)/2];
				rep(i, 1, tot)
				    ans += abs(vd - a[i]);
				cout<<ans<<endl;
			}
		}
	}
}