/*ID:PROG:LANG:*/
#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define MOD 998244353
#define MAXN 100005
using namespace std;
struct Edge{
	int to,nex;Edge(int _=0,int __=0){to=_,nex=__;}
}e[MAXN<<1];
int n,m,ans,s,t,Top,cnt,hd[MAXN],dep[MAXN],dp[MAXN],fa[MAXN],q[MAXN];
int rd(){
	int x=0,tp=1;char c=getchar();
	while(c<'0' || c>'9'){if(c=='-')tp=-1;c=getchar();}
	while(c>='0' && c<='9'){x=(x*10+c-'0');c=getchar();}
	return x*tp;
}
void adde(int x,int y){e[++cnt]=Edge(y,hd[x]);hd[x]=cnt;}
void dfs(int now,int f){
	dep[now]=dep[f]+1,fa[now]=f;
	dp[now]=(dep[now]%2)?1:-1;
	for(int i=hd[now];i;i=e[i].nex){
		if(e[i].to!=f){
			if(dep[e[i].to]){
				s=now,t=e[i].to;
				continue;
			}
			dfs(e[i].to,now);
			dp[now]+=dp[e[i].to];
		}
	}
}
void get_tree_ans(){
	if(dp[1]){printf("-1\n");exit(0);}
	for(int i=1;i<=n;i++)ans+=abs(dp[i]);
	printf("%d\n",ans);
}
void jihuan(){
	if(dp[1]%2){printf("-1\n");exit(0);}
	int tmp=-dp[1]/2;
	ans=abs(tmp);
	for(int i=s;i;i=fa[i])dp[i]+=tmp;
	for(int i=t;i;i=fa[i])dp[i]+=tmp;
	for(int i=1;i<=n;i++)ans+=abs(dp[i]);
	printf("%d\n",ans);
}
void ouhuan(){
	if(dp[1]){printf("-1\n");exit(0);}
	for(int i=t;i!=s;i=fa[i])q[++Top]=dp[i],dep[i]=0;
	sort(q+1,q+Top+1);
	int mid=q[(Top+1)/2];
	ans=abs(mid);
	for(int i=1;i<=n;i++)if(dep[i])ans+=abs(dp[i]);
	for(int i=1;i<=Top;i++)ans+=abs(q[i]-mid);
	printf("%d\n",ans);
}
int main(){
	// freopen("testdata.in","r",stdin);
	// freopen("1.out","w",stdout);
	int x,y;
	n=rd(),m=rd();
	for(int i=1;i<=m;i++){
		x=rd(),y=rd();
		adde(x,y),adde(y,x);
	}
	dfs(1,0);
	if(m==n-1){get_tree_ans();return 0;}
	if((dep[t]-dep[s])%2==0)jihuan();
	else ouhuan();
	// printf("%.2lf\n",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}