#include <bits/stdc++.h>
#define maxn 100086

using namespace std;

typedef long long ll;

int n, m;
int x, y;
vector<int> v[maxn];

int dep[maxn], fa[maxn];
ll ans, siz[maxn];
vector<ll> a;

void dfs(int i){
	dep[i] = dep[fa[i]] + 1, siz[i] = dep[i] & 1 ? 1 : -1;
	for(int j = 0;j < v[i].size();j++){
		int to = v[i][j];
		if(to == fa[i]) continue;
		if(dep[to]) x = to, y = i;
		else fa[to] = i, dfs(to), siz[i] += siz[to];
	}
	ans += abs(siz[i]);
}

int main(){
	scanf("%d%d", &n, &m);
	while(m--){
		scanf("%d%d", &x, &y);
		v[x].push_back(y), v[y].push_back(x);
	}
	x = y = 0;
	dfs(1);
	if(!x) return printf("%lld", siz[1] ? -1 : ans), 0;
	if((dep[x] - dep[y]) & 1){
		if(siz[1]) return printf("-1"), 0;
		for(int i = x;i != y;i = fa[i]){
			ans -= abs(siz[i]), a.push_back(siz[i]);
		}
		a.push_back(0);
		sort(a.begin(), a.end());
		for(int i = 0;i < a.size();i++) ans += abs(a[i] - a[a.size() / 2]);
		printf("%lld", ans);
	}else{
		if(siz[1] & 1) return printf("-1"), 0;
		ll sum = siz[1] >> 1;
		for(int i = x;i;i = fa[i]) ans -= abs(siz[i]), ans += abs(siz[i] -= sum); 
		for(int i = y;i;i = fa[i]) ans -= abs(siz[i]), ans += abs(siz[i] -= sum); 
		printf("%lld", ans + abs(sum));
	}
	
}