#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;
}

Measured memory_bytes: 13824