// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=(a),_ed=(b);i<=_ed;++i)

#define DREP(i,a,b) for(int i=(a),_ed=(b);i>=_ed;--i)

#define pb(x) push_back((x))

typedef long long ll;

inline int read(){

    register int x=0,f=1;register char ch=getchar();

    while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}

    while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}

    return f?x:-x;

}



const int N=1e5+5;

int n,m,dep[N],s,t,flg,S[N],k[N],stk[N],tp;

ll ans;

vector<int> E[N];



inline void Dfs(int u,int pa){

    for(int v:E[u]){

	if(v==pa)continue;

	if(dep[v]){flg=(dep[u]==dep[v]);s=u,t=v;continue;}

	dep[v]=dep[u]*-1;Dfs(v,u);

    }

}

inline void Cal(int u,int pa){

    for(int v:E[u]){

	if(v==pa||(u==s&&v==t)||(v==s&&u==t))continue;

	Cal(v,u);

	S[u]+=S[v];k[u]+=k[v];

    }

}



int main(){

    //freopen("in.in","r",stdin);

    n=read(),m=read();

    REP(i,1,m){int u=read(),v=read();E[u].pb(v),E[v].pb(u);}

    dep[1]=1;Dfs(1,0);

    int sum=0;

    REP(i,1,n)sum+=(S[i]=dep[i]);

    if(m==n-1){if(sum)return puts("-1"),0;}

    else if((flg&&(sum&1))||(!flg&&sum))return puts("-1"),0;

    if(m==n){

	if(flg){

	    S[s]-=sum/2,S[t]-=sum/2;

	    ans+=abs(sum/2);

	}

	else k[s]=1,k[t]=-1;

    }

    Cal(1,0);

    REP(i,1,n)

	if(!k[i])ans+=abs(S[i]);

	else if(~k[i])stk[++tp]=-S[i];

	else stk[++tp]=S[i];

    stk[++tp]=0;

    sort(stk+1,stk+tp+1);

    int x=stk[(tp+1)>>1];

    REP(i,1,tp)ans+=abs(stk[i]-x);

    printf("%lld\n",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.5764086660217957x):
#include<bits/stdc++.h>

using namespace std;



typedef long long lint;

typedef long double louble;



template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}

template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}



namespace ae86

{

	const int bufl = 1<<15;

	

	char buf[bufl],*s=buf,*t=buf;

	

	inline int fetch()

	{

		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}

		return *s++;

	}

	

	inline int ty()

	{

		int a=0,b=1,c=fetch();

		while(!isdigit(c))b^=c=='-',c=fetch();

		while(isdigit(c))a=a*10+c-48,c=fetch();

		return b?a:-a;

	}

}

using ae86::ty;



const int _ = 100007 , __ = _<<1;



int to[__],ne[__],he[_]={0},ecnt=1;

inline void adde(int a,int b){to[++ecnt]=b,ne[ecnt]=he[a],he[a]=ecnt;}

inline void addde(int a,int b){adde(a,b),adde(b,a);}



int n,m,dep[_]={0},val[_]={0},ed[_]={0},cx=0,cy=0;

lint sval=0;



void dfs(int x,int ff)

{

	ed[x]=1,dep[x]=dep[ff]+1,val[x]=(dep[x]&1)?1:-1,sval+=val[x];

	for(int i=he[x];i;i=ne[i])

	{

		int b=to[i];

		if(b==ff)continue;

		if(ed[b]){cx=x,cy=b;continue;}

		dfs(b,x);

	}

}



int dlt[_]={0};



void dfs2(int x,int ff)

{

	for(int i=he[x];i;i=ne[i])

	{

		int b=to[i];

		if(b==ff || (b==cx && x==cy) || (b==cy && x==cx))continue;

		dfs2(b,x),val[x]+=val[b],dlt[x]+=dlt[b];

	}

}



inline int treefn()

{

	if(sval!=0){puts("-1");return 0;}

	dfs2(1,0);

	lint ans=0;

	for(int i=1;i<=n;i++)ans+=abs(val[i]);

	printf("%lld\n",ans);

	return 0;

}



inline int oddcfn()

{

	if(sval&1){puts("-1");return 0;}

	sval/=2,val[cx]-=sval,val[cy]-=sval,dfs2(1,0);

	lint ans=abs(sval);

	for(int i=1;i<=n;i++)ans+=abs(val[i]);

	printf("%lld\n",ans);

	return 0;

}



int stk[_]={0},top=0;



inline int evecfn()

{

	if(sval!=0){puts("-1");return 0;}

	dlt[cx]=1,dlt[cy]=-1,dfs2(1,0);

	lint ans=0;

	stk[++top]=0;

	for(int i=1;i<=n;i++)

	{

		if(dlt[i]==0)ans+=abs(val[i]);

		else stk[++top]=-dlt[i]*val[i];

	}

	sort(stk+1,stk+top+1);

	for(int i=1,x=stk[(top+1)/2];i<=top;i++)ans+=abs(stk[i]-x);

	printf("%lld\n",ans);

	return 0;

}



int main()

{

	n=ty(),m=ty();

	for(int i=1,a,b;i<=m;i++)a=ty(),b=ty(),addde(a,b);



	dfs(1,0);



	if(m==n-1)return treefn();

	if(val[cx]==val[cy])return oddcfn();

	return evecfn();

}