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

    using namespace std;

    typedef long long LL;

     

    const int RLEN=1<<18|1;

    inline char nc() {

        static char ibuf[RLEN],*ib,*ob;

        (ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));

        return (ib==ob) ? -1 : *ib++;

    }

    inline int rd() {

        char ch=nc(); int i=0,f=1;

        while(!isdigit(ch)) {if(ch=='-')f=-1; ch=nc();}

        while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=nc();}

        return i*f;

    }

     

    const int N=1e5+50;

    int n,m,stx,sty,dep[N],fa[N],s[N];

    vector <int> edge[N]; LL ans=0;

    inline void dfs(int x,int f,int val) {

        dep[x]=dep[f]+1; fa[x]=f; s[x]=val;

        for(auto v:edge[x]) {

            if(v!=f) {

                if(dep[v]) stx=x, sty=v;

                else dfs(v,x,-val), s[x]+=s[v];

            }

        }

    }

    inline int lca(int x,int y) {

        while(dep[x]>dep[y]) x=fa[x];

        while(dep[x]<dep[y]) y=fa[y];

        while(x!=y) x=fa[x], y=fa[y];

        return x;

    }

    int main() {

        n=rd(), m=rd();

        for(int i=1;i<=m;i++) {

            int x=rd(), y=rd();

            edge[x].push_back(y);

            edge[y].push_back(x);

        } dfs(1,0,1);

        if(m==n-1) {

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

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

        } else {

            if((dep[stx]-dep[sty])&1) { // even circle

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

                static int a[N],tot;

                int l=lca(stx,sty);

                for(int u=stx;u!=l;u=fa[u]) a[++tot]=s[u],dep[u]=-1;

                for(int u=sty;u!=l;u=fa[u]) a[++tot]=-s[u],dep[u]=-1;

                a[++tot]=0; int mid=tot/2+1;

                nth_element(a+1,a+mid,a+tot+1);

                int z=a[mid];

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

                for(int i=1;i<=tot;i++) ans+=abs(a[i]-z);

            } else {

                if(s[1]&1) {puts("-1"); return 0;}

                int z=-s[1]/2; ans+=abs(z);

                for(int u=stx;u;u=fa[u]) s[u]+=z;

                for(int u=sty;u;u=fa[u]) s[u]+=z;

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

            }

        } cout<<ans<<'\n';

    }

// Faster C++ solution for the same problem (measured speedup: 6.006476712410385x):
#include<algorithm>

#include<cstring>

#include<cctype>

#include<cstdio>

#define rep(i,x,y) for(int i=x; i<=y; ++i)



using namespace std;

const int N=100005;

int n,m,cnt,h[N],siz[N],fa[N],dep[N],x,y,a[N];

struct edge{int v,n;} e[N<<1];

typedef long long LL;

LL ans;



int getint()

{

	char ch;

	while(!isdigit(ch=getchar()));

	int x=ch-48;

	while(isdigit(ch=getchar())) x=x*10+ch-48;

	return x;

}



void addedge(int u,int v)

{

	e[cnt]=(edge){v,h[u]},h[u]=cnt++;

	e[cnt]=(edge){u,h[v]},h[v]=cnt++;

}



void dfs(int x,int f,int tp)

{

	siz[x]=tp,dep[x]=dep[f]+1,fa[x]=f;

	for(int i=h[x]; i!=-1; i=e[i].n)

		if(e[i].v!=f)

		{

			if(dep[e[i].v]) ::x=e[i].v,y=x;

			else dfs(e[i].v,x,-tp),siz[x]+=siz[e[i].v];

		}

}



int main()

{

	n=getint(),m=getint();

	memset(h,-1,sizeof(h));

	rep(i,1,m) addedge(getint(),getint());

	dfs(1,0,1);

	if(m==n-1)

	{

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

		rep(i,1,n) ans+=abs(siz[i]);

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

	}

	if((dep[x]^dep[y])&1)

	{

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

		rep(i,1,n) ans+=abs(siz[i]);

		*a=1;

		while(x!=y) dep[x]>dep[y]?(a[++*a]=siz[x],ans-=abs(siz[x]),x=fa[x]):(a[++*a]=-siz[y],ans-=abs(siz[y]),y=fa[y]);

		int mid=(*a)+1>>1;

		nth_element(a+1,a+mid,a+1+*a);

		rep(i,1,*a) ans+=abs(a[i]-a[mid]);

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

	}

	else

	{

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

		int c=siz[1]>>1;

		ans=abs(c);

		while(x) siz[x]-=c,x=fa[x];

		while(y) siz[y]-=c,y=fa[y];

		rep(i,1,n) ans+=abs(siz[i]);

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

	}

	return 0;

}