#include<bits/stdc++.h>
using namespace std;
#define il inline
#define ll long long
#define rg register
#define gc getchar()
#define t(i) edge[i].to
#define rp(i,x,y) for(rg int i=x;i<=y;++i)
#define my(i,x,y) for(rg int i=x;i>=y;--i)
#define e(i,x) for(rg int i=head[x];i;i=edge[i].nxt)

const int N=1e5+10;
int n,m,head[N],ed_cnt,sz[N],st,to,sum,as,top,stck[N],g[N];
struct ed{int to,nxt;}edge[N<<1];
bool jud;

il int read()
{
    rg char ch=gc;rg int x=0;rg bool y=1;
    while(ch!='-' && (ch>'9' || ch<'0'))ch=gc;
    if(ch=='-')ch=gc,y=0;
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc;
    return y?x:-x;
}
il void ad(int x,int y){edge[++ed_cnt]=(ed){x,head[y]};head[y]=ed_cnt;}
void dfs1(int x,int fa){e(i,x)if(t(i)^fa)if(sz[t(i)]){if(sz[t(i)]==sz[x])jud=1;st=x;to=t(i);}else sz[t(i)]=-sz[x],dfs1(t(i),x);}
void dfs2(int x,int fa){e(i,x)if(t(i)^fa && (!(x==st && t(i)==to) || !(x==to && t(i)==st)))dfs2(t(i),x),sz[x]+=sz[t(i)],g[x]+=g[t(i)];return;}

int main()
{
// 	freopen("damori.in","r",stdin);freopen("damori.out","w",stdout);
	n=read();m=read();rp(i,1,m){int x=read(),y=read();ad(x,y);ad(y,x);}sz[1]=1;dfs1(1,0);
	rp(i,1,n)sum+=sz[i];if(!jud)if(sum)return printf("-1\n"),0;
	if(m==n)if(jud){if(sum&1)return printf("-1\n"),0;as+=abs(sum>>1);sz[st]-=(sum>>1);sz[to]-=(sum>>1);}else g[st]=1,g[to]=-1;
	dfs2(1,0);
	rp(i,1,n){if(g[i])stck[++top]=g[i]*sz[i];else as+=abs(sz[i]);}
    stck[++top]=0;sort(stck+1,stck+top+1);int mid=stck[(top+1)>>1];
    rp(i,1,top)as+=abs(stck[i]-mid);
	printf("%lld\n",as);
    return 0;
}
