#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<P,int> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())

int n,m;
vector<int>edge[100005];
ll sum[100005][2];
int C[100005];
void dfs(int v,int u,int col){
	sum[v][0] = col;
	sum[v][1] = 1-col;
	C[v] = col;
	for(int i=0;i<edge[v].size();i++){
		if(edge[v][i]==u) continue;
		dfs(edge[v][i],v,1-col);
		rep(j,2) sum[v][j]+=sum[edge[v][i]][j];
	}
}
void sol_tree(){
	rep(i,m){
		int a,b; cin>>a>>b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	dfs(1,-1,0);
	if(sum[1][0] != sum[1][1]) {cout<<-1<<endl;exit(0);}
	ll ans = 0;
	for(int i=2;i<=n;i++) ans += abs(sum[i][0]-sum[i][1]);
	cout<<ans<<endl;
}
int par[100005],ran[100005];
void init(){ for(int i=0;i<100005;i++) par[i] = i; }
int find(int x){ if(x == par[x]) return x; else return par[x] = find(par[x]); }
void unite(int x,int y){
	x = find(x); y = find(y); if(x==y) return;
	if(ran[x] < ran[y]) par[x] = y;
	else{
		par[y] = x;
		if(ran[x] == ran[y]) ran[x]++;
	}
}
bool same(int x,int y){ return find(x)==find(y); }
int X,Y;
ll cur_param;
ll S[100005],summ;
ll dfss(int v,int u,int col){
	S[v] = C[v];
	if(v == X) S[v] += cur_param;
	if(v == Y) S[v] -= cur_param;
	ll target = 1-C[v];
	for(int i=0;i<edge[v].size();i++){
		if(edge[v][i]==u) continue;
	    target += dfss(edge[v][i],v,1-col);
		S[v] += S[edge[v][i]];
	}
//	if(cur_param == 0) cout<<target << " " << S[v] << " " << v << endl;
	if(v != 1) summ += abs(target-S[v]);
	return target;
}
ll dfsss(int v,int u,int col){
	S[v] = C[v];
	if(v == X) S[v] += cur_param;
	if(v == Y) S[v] += cur_param;
	ll target = 1-C[v];
	for(int i=0;i<edge[v].size();i++){
		if(edge[v][i]==u) continue;
		target += dfsss(edge[v][i],v,1-col);
		S[v] += S[edge[v][i]];
	}
	if(v != 1) summ += abs(target-S[v]);
	return target;
}
void sol_namori(){
	//todo
	init();
	rep(i,m){
		int a,b; cin>>a>>b;
		if(!same(a,b)){
			edge[a].pb(b);
			edge[b].pb(a);
		}
		else{
			X = a; Y = b; 
		}
		unite(a,b);
	}
	dfs(1,-1,0);
	if(C[X] != C[Y]){
		//この辺を何回使うかを決め打ちすると下に凸になる
		if(sum[1][0] != sum[1][1]){
			puts("-1");
			exit(0);
		}
		ll lb = -1e9,ub = 1e9;
		while(ub-lb > 2){
			ll x = (lb*2+ub)/3;
			ll y = (lb+ub*2)/3;
			cur_param = x; summ = 0;
			dfss(1,-1,0);
			ll zip_x = summ;
			cur_param = y; summ = 0;
			dfss(1,-1,0);
			ll zip_y = summ;
			if(zip_x < zip_y){
				ub = y;
			}
			else{
				lb = x;
			}
		}
		ll ans = 1e18;
		for(ll x=lb-5;x<=lb+5;x++){
			cur_param = x; summ = 0; dfss(1,-1,0); ans = min(ans,summ+abs(x));
		}
		cout << ans << endl;
	}
	else{
		//X-Yで辻褄を合わせて、和を取るだけ
		if(sum[1][0]%2 != sum[1][1]%2){
			puts("-1");
			exit(0);
		}
		int x = sum[1][1]-sum[1][0];
		x /= 2; 
		cur_param = x; 
		summ = 0;
		dfsss(1,-1,0);
		cout << summ+abs(x) << endl;
	}
	return;
}
int main(){
	cin>>n>>m;
	if(m == n-1) sol_tree();
	else sol_namori();
	return 0;
}


Measured memory_bytes: 16512