#include <bits/stdc++.h>
#define pb push_back
#define cmin(a,b) (a>b?a=b:a)
#define cmax(a,b) (a<b?a=b:a)
#define mem(a,k) memset(a,k,sizeof(a))
#define lop(i,s,t) for(int i=s;i<(t);++i)
#define rep(i,s,t) for(int i=s;i<=(t);++i)
#define dec(i,s,t) for(int i=s;i>=(t);--i)
#define fore(i,v) for(int i=g[v],d=es[i].d;i;i=es[i].nxt,d=es[i].d)
using namespace std;

typedef long long ll;

template<typename T>
void read(T &x){
	x=0; 
	char c;
	for(c=getchar();!isdigit(c);c=getchar()); 
	for(;isdigit(c);c=getchar())x=x*10+c-'0'; 
}

const int N=1e5+50; 

int m,ans,n,sum[N],dep[N],prt[N],X,Y;
vector<int> g[N];

void dfs(int v,int p,int c){
	sum[v]=c;
	prt[v]=p; 
	dep[v]=dep[p]+1; 
	for(int d:g[v])if(d!=p){
		if(dep[d])X=v,Y=d;
		else{
			dfs(d,v,-c); 
			sum[v]+=sum[d];
		}
	}
}

int lca(int u,int v){
	while(u!=v){
		if(dep[u]>dep[v])u=prt[u];
		else v=prt[v];
	}
	return u;
}

void solveEven(){
	if(sum[1]!=0){
		puts("-1"); 
		exit(0); 
	}
	int L=lca(X,Y);
	vector<int> a;
	for(;X!=L;X=prt[X])a.pb(sum[X]),sum[X]=0;
	for(;Y!=L;Y=prt[Y])a.pb(sum[Y]),sum[Y]=0;
	a.pb(0); 
	nth_element(a.begin(),a.begin()+a.size()/2,a.end()); 
	int mid=a[a.size()/2];
	for(int x:a)ans+=abs(x-mid);
	rep(i,1,n)ans+=abs(sum[i]); 
}

void solveOdd(){
	//printf("Solve Odd\n");
	if(sum[1]%2!=0){
		puts("-1"); 
		exit(0); 
	}
	int k=-sum[1]/2;
	ans+=abs(k);
	for(;X;X=prt[X])sum[X]+=k;
	for(;Y;Y=prt[Y])sum[Y]+=k;
	rep(i,1,n)ans+=abs(sum[i]);
}

int main(int argc,char *argv[]){
#ifdef CURIOUSCAT
	//freopen("dat.in","r",stdin);
	//freopen("my.out","w",stdout);
#endif
	read(n),read(m);
	rep(i,1,m){
		int u,v;
		read(u),read(v); 
		g[u].pb(v);
		g[v].pb(u);
	}
	dfs(1,0,1);
	if(m==n-1){
		if(sum[1]!=0){
			puts("-1"); 
			return 0; 
		}
		rep(i,1,n)ans+=abs(sum[i]); 
	}else{
		if(abs(dep[X]-dep[Y])&1)solveEven(); 
		else solveOdd();
	}
	printf("%d\n",ans);
	return 0;
}