// Original C++ solution (slower):
#pragma GCC optimize("Ofast")

#pragma GCC optimize("inline", "fast-math", "unroll-loops", "no-stack-protector")

#pragma GCC diagnostic error "-fwhole-program"

#pragma GCC diagnostic error "-fcse-skip-blocks"

#pragma GCC diagnostic error "-funsafe-loop-optimizations"

#include<vector>

#include<cstdio>

#include<cstring>

#include<cassert>

#include<map>

#include<algorithm>

#include<cmath>

#include<queue>

#define ll long long

using namespace std;

namespace io {

	const int SIZE = (1 << 21) + 1;

	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55];

	int f, qr;

#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)

	inline void flush () {

		fwrite (obuf, 1, oS - obuf, stdout);

		oS = obuf;

	}

	inline void putc (char x) {

		*oS ++ = x;

		if (oS == oT) flush ();

	}

	template<class T>

	inline void getc(T &x)

	{

		for(c=gc();!((c>='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z'));c=gc());

		x=c;

	}

	template <class I>

	inline void rd (I &x) {

		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;

		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15);

		x *= f;

	}

	template <class I>

	inline void print (I x) {

		if (!x) putc ('0');

		if (x < 0) putc ('-'), x = -x;

		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;

		while (qr) putc (qu[qr --]);

	}

	struct Flusher_ {

		~Flusher_() {

			flush();

		}

	} io_flusher_;

}

using io :: rd;

using io :: putc;

using io :: print;

using io :: getc;

const int MAXN=1e5+5;

int n,m;

vector<int>g[MAXN];

namespace Tree

{

    int ans;

    int dfs(int x,int ff,int col)

    {

        int res=col;

        for(auto &v:g[x])

        {

            if(v==ff) continue;

            res+=dfs(v,x,-col);

        }

        ans+=abs(res);

        return res;

    }

    void solve()

    {

        ans=0;

        if(dfs(1,0,1))

            puts("-1");

        else printf("%d\n",ans);

    }

}

int col[MAXN],U,V;

void dfs(int x,int ff,int c)

{

    col[x]=c;

    for(auto &v:g[x])

    {

        if(v==ff) continue;

        if(!col[v]) dfs(v,x,-c);

        else U=x,V=v;

    }

}

namespace No_Tree1

{

    int Ans;

    int dfs(int x,int ff)

    {

        int res=col[x];

        for(auto &v:g[x])

        {

            if(v==ff||(U==x&&V==v)||(U==v&&V==x))

                continue;

            res+=dfs(v,x);

        }

        return res;

    }

    int rdfs(int x,int ff)

    {

        

        for(auto &v:g[x])

        {

            if(v==ff||(U==x&&V==v)||(U==v&&V==x))

                continue;

            col[x]+=rdfs(v,x);

        }

        Ans+=abs(col[x]);

        return col[x];

    }

    void solve()

    {

        Ans=dfs(1,0);

        if(Ans%2)

        {

            puts("-1");

            return;

        }

       // fprintf(stderr,"%d\n",Ans);

        col[U]-=Ans/2;

        col[V]-=Ans/2;

        Ans=abs(Ans/2);

        rdfs(1,0);

        printf("%d\n",Ans);



    }

}

namespace No_Tree2

{

    int Ans,fa[MAXN];

    void dfs(int x,int ff,int c)

    {

        col[x]=c;fa[x]=ff;

        for(auto &v:g[x])

        {

            if(v==ff||(U==x&&V==v)||(U==v&&V==x))

                continue;

            dfs(v,x,-c);

            col[x]+=col[v];

        }

    }

    int st[MAXN],tp,del;

    bool is[MAXN];

    void solve()

    {

        dfs(1,0,-1);

        if(col[1])

        {

            puts("-1");

            return;

        }

        tp=0;

        for(int i=1;i<=n;i++) is[i]=0;

        for(int i=V;i!=U&&i;i=fa[i])

            st[++tp]=col[i],is[i]=1;

      //  printf("%d %d\n",U,V);

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

        if(tp&1) del=st[tp+1>>1];

        else del=st[tp>>1]+st[tp/2+1]>>1;

        Ans=abs(del);

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

        if(is[i]) Ans+=abs(col[i]-del);

        else Ans+=abs(col[i]);

        printf("%d\n",Ans);

    }

}

int T;

int main()

{T=1;

    while(T--)

    {

        rd(n);rd(m);

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

            col[i]=0,g[i].clear();

        for(int i=1,x,y;i<=m;i++)

        {

            rd(x);rd(y);

            g[x].push_back(y);

            g[y].push_back(x);

        }

        if(n&1)

        {

            puts("-1");

            continue;

        }

        if(m==n-1)

        {

            Tree::solve();

            continue;

        }

        U=V=0;

        dfs(1,0,1);

        if(col[U]==col[V])

            No_Tree1::solve();

        else No_Tree2::solve();

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define LL long long

#define il inline



using namespace std;

const int N=1e5+10;

il int rd()

{

    int x=0,w=1;char ch=0;

    while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}

    while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}

    return x*w;

}

int to[N<<1],nt[N<<1],hd[N],dg[N],ddg[N],tot;

void add(int x,int y)

{

    ++tot,to[tot]=y,nt[tot]=hd[x],hd[x]=tot,++dg[x];

    ++tot,to[tot]=x,nt[tot]=hd[y],hd[y]=tot,++dg[y];

}

int n,m,ans,s[N],de[N],sx,sy;

bool v[N];

int st[N],tt,sq[N],ts,kk[N];

void findd()

{

    queue<int> q;

	for(int i=1;i<=n;++i) if(dg[i]==1) q.push(i);

	while(!q.empty())

	{

		int x=q.front();

		q.pop();

		for(int i=hd[x];i;i=nt[i])

		{

			int y=to[i];

			--dg[y];

			if(dg[y]==1) q.push(y);

		}

	}

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

		if(dg[i]>1)

		{

			int la=0,x=i;

			while(1)

			{

				sq[++ts]=x;

				for(int i=hd[x];i;i=nt[i])

				{

					int y=to[i];

					if(dg[y]<=1||y==la) continue;

					la=x,x=y;break;

				}

				if(x==i) break;

			}

			return;

		}

}

void dfs(int x,int ffa,int ss)

{

	if(x==sx) kk[x]=1;

	else if(x==sy) kk[x]=-1;

	s[x]+=de[x]=ss;

	for(int i=hd[x];i;i=nt[i])

	{

		int y=to[i];

		if(y==ffa) continue;

		if(min(x,y)==min(sx,sy)&&max(x,y)==max(sx,sy)) continue;

		dfs(y,x,-ss);

		s[x]+=s[y],kk[x]+=kk[y];

	}

	ans+=abs(s[x]);

}



int main()

{

/////

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

    for(int i=1;i<=m;++i) add(rd(),rd());

    if(m==n-1)

    {

		dfs(1,0,1);

		printf("%d\n",s[1]?-1:ans);

    }

    else

    {

		findd();

		sx=sq[1],sy=sq[2];

		dfs(1,0,1);

		if(ts&1)

		{

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

			else

			{

				int sht=-(s[1]/2);

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

				s[sq[1]]=s[sq[2]]=sht;

				ans=0,dfs(1,0,1);

				printf("%d\n",ans+abs(sht));

			}

		}

		else

		{

			if(s[1]) puts("-1");

			else

			{

				ans=0;

				st[tt=1]=0;

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

				{

					if(!kk[i]) ans+=abs(s[i]);

					else st[++tt]=(~kk[i])?-s[i]:s[i];

				}

				sort(st+1,st+tt+1);

				for(int i=1;i<=tt;++i) ans+=abs(st[i]-st[(tt+1)/2]);

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

			}

		}

    }

    return 0;

}

// Fastest known solution for this problem: