#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define repu(i,x,y) for (int i=x; i<=y; ++i)
#define repe(i,x) for (edge *i=fst[x]; i; i=i->nxt)
using namespace std;

typedef long long LL;
int n,m,u,v,dep[100100],w[100100],a[100100],tot;
LL ans;
struct edge
{
    int v;
    edge *nxt;
} pool[200100],*tp=pool,*fst[100100];

int getint()
{
    char ch;
    while (!isdigit(ch=getchar()));
    int x=ch-'0';
    for (; isdigit(ch=getchar()); x=x*10+ch-'0');
    return x;
}

void dfs1(int x,int fa)
{
    dep[x]=dep[fa]+1,w[x]=-w[fa];
    repe(i,x)
        if (i->v!=fa)
            if (dep[i->v])
                u=x,v=i->v;
            else
                dfs1(i->v,x);
}

bool dfs2(int x,int fa)
{
    bool flag=x==u || x==v;
    dep[x]=1;
    repe(i,x)
        if (!dep[i->v])
            flag^=dfs2(i->v,x),w[x]+=w[i->v];
    if (flag)
        a[++tot]=w[x];
    else
        ans+=abs(w[x]);
    return flag;
}

int main()
{
    scanf("%d%d",&n,&m);
    repu(i,1,m)
    {
        u=getint(),v=getint();
        *tp=(edge){v,fst[u]},fst[u]=tp++;
        *tp=(edge){u,fst[v]},fst[v]=tp++;
    }
    if (n&1)
    {
        puts("-1");
        return 0;
    }
    w[0]=1,dfs1(1,0);
    int sum=0;
    repu(i,1,n)
        sum+=w[i];
    if (sum && (m<n || ((dep[u]-dep[v])&1)))
    {
        puts("-1");
        return 0;
    }
    if (m<n)
        u=v=0;
    if (m==n && !((dep[u]-dep[v])&1))
        w[u]-=sum/2,w[v]-=sum/2,ans=abs(sum/2),u=v=0;
    memset(dep,0,sizeof(dep));
    tot=1,dfs2(u?u:1,0),nth_element(a+1,a+tot/2,a+1+tot);
    repu(i,1,tot)
        ans+=abs(a[i]-a[tot/2]);
    printf("%lld\n",ans);
    return 0;
}