#include<bits/stdc++.h>
#define RG register
#define IL inline
#define int long long
#define DB double
using namespace std;

int gi() {
    char ch=getchar(); int x=0,q=0;
    while(ch<'0'||ch>'9') q=ch=='-'?1:q,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return q?-x:x;
}

const int N=1e5+10;

int n,m,ans,tot,cnt,s[N],fa[N],flag[N],head[N],size[N];

struct Edge{int next,to;}e[N<<1];
IL void make(int a,int b) {
	e[++tot]=(Edge){head[a],b},head[a]=tot;
	e[++tot]=(Edge){head[b],a},head[b]=tot;
}

int getfa(int x) {return x==fa[x]?x:fa[x]=getfa(fa[x]);}

void dfs(int x,int fx,int fl) {
	RG int i,y;
	fa[x]=fx,size[x]=flag[x]=fl;
	for (i=head[x];i;i=e[i].next)
		if ((y=e[i].to)!=fx) 
			dfs(y,x,-fl),size[x]+=size[y];
}

signed main ()
{
	RG int i,x,y,fx,fy,Fr,To,now;
	n=gi(),m=gi();
	for (i=1;i<=n;++i) fa[i]=i;
	for (i=1;i<=m;++i) {
		x=gi(),y=gi(),fx=getfa(x),fy=getfa(y);
		if (fx==fy) Fr=x,To=y;
		else fa[fx]=fy,make(x,y);
	}
	dfs(1,0,1);
	if (m==n-1) {
		if (size[1]) return puts("-1"),0;
	}
	else if (flag[x]==flag[y]) {
		if (size[1]&1) return puts("-1"),0;
		now=size[1]/2,ans+=abs(now);
		for (i=Fr;i;i=fa[i]) size[i]-=now;
		for (i=To;i;i=fa[i]) size[i]-=now;		
	}
	else {
		if (size[1]) return puts("-1"),0;
		s[cnt=1]=0;
		for (i=Fr;fa[i];i=fa[i]) s[++cnt]=-size[i];
		for (i=To;fa[i];i=fa[i]) s[++cnt]=size[i];
		sort(s+1,s+cnt+1);
		now=s[cnt+1>>1],ans+=abs(now);
		for (i=Fr;fa[i];i=fa[i]) size[i]+=now;
		for (i=To;fa[i];i=fa[i]) size[i]-=now;
	}
	for (i=1;i<=n;++i) ans+=abs(size[i]);
	printf("%lld\n",ans);
    return 0;
}
