#include <bits/stdc++.h>
using namespace std;
int h[500001],w[500001],o,q[500001],f[500001],c[500001],v[500001],qw,wq,a,b,qq=1,ww=1,ans=0;
int get(int y)
{
    if(y==f[y]) return y ; 
    return f[y]=get(f[y]);
}
void dfs(int n, int m)
{
    c[n]=m;
    for (int i=h[n]; i; i=w[i])
    {
        if (q[i]!=f[n])
        {
            f[q[i]]=n;
            dfs(q[i], -m);
            c[n] += c[q[i]];
        }
    }
}
int main()
{
    for (int i = 1; i <= 500000; ++i)
    {
        f[i] = i;
    }
    cin>>a>>b;
    for (int i = 1; i <= b; ++i)
    {
        cin>>qw>>wq;
        if (get(qw) != get(wq))
        {
            f[f[qw]] = f[wq];
        }
        else
        {
            qq = qw;
             ww = wq;
            continue;
        }
        o++;
        w[o] = h[qw];
        q[o] = wq;
        h[qw]=o;
        o++;
        w[o] = h[wq];
        q[o] = qw;
        h[wq]=o;
    }
    f[qq]=0;
    dfs(qq,1);
    if(a==b)
    {
        long long u=0;
        for(int i=ww;i;i=f[i])
        {
            u++;
            v[u]=c[i];
        }
        if(u%2)
        {
            if(c[qq]%2)
            {
                cout<<"-1";
                return 0;
            }
            for (int i = ww; i; i = f[i])
            {
                c[i] -= c[qq] / 2;
            }
        }
        else
        {
            if (c[qq])
            {
                cout<<"-1";
                return 0;
            }
            sort(v + 1, v + u + 1);
            for (int i = ww; i; i = f[i])
            {
                c[i] -= v[u / 2];
            }
        }
    }
    else if (c[qq])
    {
        cout<<"-1";
        return 0;
    }
    for (int i = 1; i <= a; ++i)
    {
        ans += abs(c[i]);
    }
    cout<<ans;
    return 0;
}