// Original C++ solution (slower):
#include<bits/stdc++.h>

#define pb push_back

using namespace std;

const int N=1e5+10;

typedef long long ll;

int gi() {

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

    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();

    if(ch=='-') o=-1,ch=getchar();

    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();

    return x*o;

}

int n,m,a,b,s[N],dep[N],st[N],fa[N],top=0;

vector<int> E[N];

ll ans=0;

void dfs(int u,int ff,int val) {

    fa[u]=ff;dep[u]=dep[ff]+1;s[u]=val;

    for(int v:E[u]) {

        if(v==ff) continue;

        if(!dep[v]) dfs(v,u,-val),s[u]+=s[v];

        else a=u,b=v;

    }

}

int main() {

    cin>>n>>m;

    for(int i=1,u,v;i<=m;i++) u=gi(),v=gi(),E[u].pb(v),E[v].pb(u);

    dfs(1,0,1);

    if(dep[a]<dep[b]) swap(a,b);

    if(m==n-1) {

        if(s[1]) return puts("-1"),0;

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

    }

    else if((dep[a]-dep[b])&1) {

        if(s[1]) return puts("-1"),0;

        for(int p=a;p!=b;p=fa[p]) st[++top]=s[p],dep[p]=-1;

        st[++top]=0;

        int mid=(top+1)>>1;nth_element(st+1,st+mid,st+top+1);

        for(int i=1;i<=n;i++) if(dep[i]!=-1) ans+=abs(s[i]);

        for(int i=1;i<=top;i++) ans+=abs(st[mid]-st[i]);

    }

    else {

        if(abs(s[1])&1) return puts("-1"),0;

        int z=s[1]/2;

        for(int p=a;p;p=fa[p]) s[p]-=z;

        for(int p=b;p;p=fa[p]) s[p]-=z;

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

        ans+=abs(z);

    }

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

    return 0;

}

//orzgzy

//鸡贼明年进队超稳


// Optimized solution to the same problem (faster):
#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();

}


// Fastest known solution for this problem: