#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define I int
#define GG(x) if(x)printf("-1\n"),exit(0);
const I N=1e6+5;struct G{I o,r;}e[N];I h[N],w=1;E(I x,I y){e[w]=(G){y,h[x]};h[x]=w++;}I S[N],d[N],K[N],V[N],X,Y,L,T=0,Q[N],P;ll A=0;D(I u,I f){V[u]=((d[u]&1)?1:-1);P+=V[u];for(I i=h[u];i;i=e[i].r){I v=e[i].o;if(v^f)d[v]?(X=u,Y=v,L=abs(d[u]-d[v])+1):(d[v]=d[u]+1,D(v,u),0);}}D2(I u,I f){S[u]=V[u];for(I i=h[u];i;i=e[i].r){I v=e[i].o;if(!(v==f||(u==X&&v==Y)||(u==Y&&v==X)))D2(v,u),S[u]+=S[v],K[u]+=K[v];}K[u]?(Q[++T]=K[u]*S[u]):(A+=abs(S[u]));}main(){I n,m;scanf("%d%d",&n,&m);for(I i=1;i<=m;i++){I x,y;scanf("%d%d",&x,&y),E(x,y),E(y,x);}d[1]=1;D(1,1);if(m==n-1){GG(P)}else if(L&1){GG(P&1)A+=abs(P)/2,V[X]-=P/2,V[Y]-=P/2;}else {GG(P)K[X]++,K[Y]--;}D2(1,1);Q[++T]=0;sort(Q+1,Q+T+1);for(I i=1;i<=T;i++)A+=abs(Q[(T+1)/2]+Q[i]);printf("%lld\n",A);}
