#include<bits/stdc++.h>
using namespace std;

const int N=200100;
typedef long long ll;
ll ans;

int b[N],a[N],col[N],c[N],fa[N],vis[N],G[N],ne[N*2],to[N*2],n,m,xb;

void dfs(int x,int fa=-1){
	for(int i=G[x];~i;i=ne[i])if(to[i]!=fa){
		dfs(to[i],x);
		col[x]-=col[to[i]];
	}
	++col[x];ans+=abs(col[x]);	
}

int cnt;
void find_cir(int x,int y){
	assert(x);
	c[++cnt]=x;if(x==y)return;
	find_cir(fa[x],y);
}

void tj(int x){
	vis[x]=1;
	for(int i=G[x];~i;i=ne[i])if(to[i]!=fa[x]){
		if(!cnt&&vis[to[i]])find_cir(x,to[i]);
		else if(!vis[to[i]])fa[to[i]]=x,tj(to[i]);
	}
}
void add(int x,int y){ne[xb]=G[x];to[xb]=y;G[x]=xb++;}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m;
	if(n&1){
		cout<<-1<<'\n';
		return 0;
	}
	memset(G,-1,sizeof G);
	for(int i=1;i<=m;++i){
		int x,y;cin>>x>>y;
		add(x,y);add(y,x);
	}
	if(m==n-1){
		dfs(1);
		if(col[1])cout<<-1<<'\n';
		else cout<<ans<<'\n';
		return 0;
	}
	tj(1);
	c[0]=c[cnt];c[cnt+1]=c[1];
	for(int i=1;i<=cnt;++i){
		a[i]=1;
		for(int j=G[c[i]];~j;j=ne[j])if(to[j]!=c[i-1]&&to[j]!=c[i+1]){dfs(to[j],c[i]);a[i]-=col[to[j]];}
	}
	int nx=1,ny=0;
	for(int i=1;i<=cnt;++i){
		nx=-nx;ny=a[i]-ny;
		if(nx==1)b[i]=-ny;
		else b[i]=ny;
	}
	if(nx==1&&ny){
		cout<<-1<<'\n';
		return 0;
	}
	if(nx==-1&&(ny&1)){
		cout<<-1<<'\n';
		return 0;
	}
	sort(b+1,b+cnt+1);
	int val=b[(cnt+1)/2];
	if(nx==-1)val=ny/2;
	for(int i=1;i<=cnt;++i)ans+=abs(val-b[i]);
	cout<<ans<<'\n';	
	return 0;
}
