#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define rep(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int N=100010;
const ll INF=1000000000000000ll;
//const int N=20;
int n,m;

struct substack1{	
	struct node{
		int to,next;
	};node edge[N*2];
	int graph[N],tot;
	void addedge(int i,int x,int y){
		edge[i].to=y;edge[i].next=graph[x];graph[x]=i;
	}
	int dp[N];int fa[N];ll ans;
	void dfs(int x){
		int i,y;
		for(i=graph[x];i;i=edge[i].next){
			y=edge[i].to;
			if(y==fa[x])	continue;
			fa[y]=x;dfs(y);
			dp[x]+=(1-dp[y]);ans+=abs(1-dp[y]);
		}
	}
	void solve(){
		int i,x,y;
		rep(i,1,m){
			scanf("%d%d",&x,&y);
			tot++;addedge(tot,x,y);
			tot++;addedge(tot,y,x);
		}
		dfs(1);
		//rep(i,1,n)	cerr<<dp[i]<<endl;
		if(dp[1]!=1)	printf("-1\n");
		else printf("%lld\n",ans);
	}
};substack1 s1;

struct substack2{
	struct node{
		int to,next;
	};node edge[N*2];
	int graph[N],tot;
	void addedge(int i,int x,int y){
		edge[i].to=y;edge[i].next=graph[x];graph[x]=i;
	}
	int ex[N],ey[N];
	int dp[N],fa[N];int vis[N];int need[N];
	int b[N],cb;ll sum[N];
	int a[N],cnt;
	int col[N],num[3];int flag;	
	ll ans;int cir[N];
	void find(int x){
		int i,y;vis[x]=1;
		for(i=graph[x];i;i=edge[i].next){
			y=edge[i].to;
			if(y==fa[x])	continue;
			if(vis[y]){
				flag=1;
				while(x!=y){a[++cnt]=x;x=fa[x];}
				a[++cnt]=y;
				return;
			}
			fa[y]=x;find(y);
			if(flag)	return;
		}
	}
	void dfs(int x){
		int i,y;
		for(i=graph[x];i;i=edge[i].next){
			y=edge[i].to;
			if(y==fa[x]||cir[y])	continue;
			fa[y]=x;dfs(y);
			dp[x]+=(1-dp[y]);ans+=abs(1-dp[y]);
		}
	}
	void color(int x){
		num[col[x]]++;int i,y;
		for(i=graph[x];i;i=edge[i].next){
			y=edge[i].to;
			if(y==fa[x])	continue;
			fa[y]=x;col[y]=col[x]^1;color(y);
		}
	}
	void work1(){
		int i,x,y;
		//rep(i,1,cnt)	cerr<<a[i]<<' ';cerr<<endl;
		memset(graph,0,sizeof(graph));tot=0;
		memset(fa,0,sizeof(fa));
		rep(i,1,m){
			x=ex[i];y=ey[i];
			if((x<y)!=(a[1]<a[2]))	swap(x,y);
			if(x==a[1]&&y==a[2])	continue;
			//cerr<<x<<' '<<y<<endl;
			tot++;addedge(tot,x,y);
			tot++;addedge(tot,y,x);
		}
		color(a[1]);
		//rep(i,1,n)	cerr<<col[i]<<' ';cerr<<endl;
		if((num[0]-num[1])&1)	{printf("-1\n");return;}
		x=(num[0]-num[1])/2;
		dp[a[1]]=x;dp[a[2]]=x;
		ans=abs(x);
		dfs(a[1]);
		printf("%lld\n",ans);
	}
	void work2(){
		int i;//n/=0;
		memset(fa,0,sizeof(fa));
		rep(i,1,cnt)	cir[a[i]]=1;
		rep(i,1,cnt)	dfs(a[i]);
		rep(i,1,cnt)	need[a[i]]=1-dp[a[i]];
		a[cnt+1]=a[1];
		need[a[1]]+=0;need[a[2]]+=0;b[++cb]=0;
		rep(i,2,cnt){
			if(i&1)	b[++cb]=need[a[i]];
			else b[++cb]=-need[a[i]];
			need[a[i+1]]-=need[a[i]];need[a[i]]=0;
		}
		//rep(i,1,cb)	cerr<<b[i]<<' ';cerr<<endl;
		ll mx=INF;
		//cerr<<need[a[1]];
		if(need[a[1]]!=0){printf("-1\n");return;}	
		sort(b+1,b+1+cb);rep(i,1,cb)	sum[i]=sum[i-1]+b[i];
		rep(i,1,cb)	if(i==1||b[i]!=b[i-1]){
			//mx=min(mx,1ll*b[i]*i-sum[i]+sum[cb]-sum[i]-1ll*b[i]*(cb-i));
			mx=min(mx,sum[cb]-2*sum[i]-1ll*b[i]*(cb-2*i));
		}
		printf("%lld\n",ans+mx);
	}
	void solve(){
		int i,x,y;
		rep(i,1,m){
			scanf("%d%d",&ex[i],&ey[i]);
			x=ex[i];y=ey[i];
			tot++;addedge(tot,x,y);
			tot++;addedge(tot,y,x);
		}
		find(1);
		if(cnt&1)	work1();
		else work2();
	}
};substack2 s2;

int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	int i,x,y;
	scanf("%d%d",&n,&m);
	if(m==n-1)	s1.solve();
	else s2.solve();
}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

Measured memory_bytes: 10624