#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 100005;
struct Edge{
	int to,next;
}e[MAXN<<1];
int head[MAXN],cnt,s[MAXN],n,m,sx,sy,fa[MAXN];
inline void insert(int u,int v)
{
	e[++cnt].to=v,e[cnt].next=head[u],head[u]=cnt;
	e[++cnt].to=u,e[cnt].next=head[v],head[v]=cnt;
}
int color[MAXN],t[MAXN],tot;
int dep[MAXN];
inline void bicolor(int x,int fa)
{
	tot+=color[x];	
	for(int i=head[x];i;i=e[i].next){
		int v=e[i].to;
		if(v==fa)continue;
		dep[v]=dep[x]+1;
		color[v]=-color[x];
		bicolor(v,x);
	}
}
int ans=0;
void solve(int x,int fa)
{
	for(int i=head[x];i;i=e[i].next){
		int v=e[i].to;
		if(v==fa)continue;
		solve(v,x);
		color[x]+=color[v];
	}
}
int k[MAXN];
void getk(int x,int fa)
{
	for(int i=head[x];i;i=e[i].next){
		int v=e[i].to;
		if(v==fa)continue;
		getk(v,x);
		k[x]+=k[v];
	}
}
inline int getan(int x)
{
	return x==fa[x]?x:fa[x]=getan(fa[x]);
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)fa[i]=i;
	for(int i=1,u,v,x,y;i<=m;++i){
		scanf("%d%d",&u,&v);
		x=getan(u),y=getan(v);
		if(x!=y){
			fa[x]=y;
			insert(u,v);
		}else{
			sx=u,sy=v;
		}
	}
	color[1]=1;
	bicolor(1,0);
	if(m==n-1){
		if(tot){puts("-1");return 0;}
		else{
			solve(1,0);
			for(int i=1;i<=n;++i)
				ans+=abs(color[i]);
			cout<<ans<<endl;
		}
	}else{
		int size=abs(dep[sx]-dep[sy])+1;
		if(size&1){
			if(tot%2){puts("-1");return 0;}
			else{
				color[sx]+=-tot/2,color[sy]+=-tot/2;
				solve(1,0);
				for(int i=1;i<=n;++i)
					ans+=abs(color[i]);
				cout<<ans+abs(tot/2)<<endl;
			}
		}else{
			if(tot){puts("-1");return 0;}
			k[sx]=-1,k[sy]=1;
			getk(1,0);		  
			solve(1,0);
			for(int i=1;i<=n;++i){
				if(!k[i])ans+=abs(color[i]);
				else{
					t[++tot]=-k[i]*color[i];
				}
			}
			sort(t+1,t+tot+1);
			int x=t[(tot+1)/2];
			for(int i=1;i<=n;++i){
				if(k[i])ans+=abs(k[i]*x+color[i]);
			}
			cout<<ans+abs(x)<<endl;
		}
	}
	return 0;
}
