#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=100005;

int n,m,cnt,last[N],dep[N],len,sx,sy,a[N],odd,even,s[N],k[N];
bool vis[N];
struct edge{int to,next;}e[N*2];

int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void addedge(int u,int v)
{
    e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
    e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;
}

void dfs1(int x,int fa)
{
    vis[x]=1;
    if (dep[x]&1) even++;
    else odd++;
    for (int i=last[x];i;i=e[i].next)
    {
        if (e[i].to==fa) continue;
        if (vis[e[i].to]&&dep[e[i].to]<dep[x]) len=dep[x]-dep[e[i].to]+1,sx=x,sy=e[i].to;
        else if (!vis[e[i].to]) dep[e[i].to]=dep[x]+1,dfs1(e[i].to,x);
    }
}

void dfs2(int x)
{
    vis[x]=1;
    for (int i=last[x];i;i=e[i].next)
        if (!vis[e[i].to]) dfs2(e[i].to),s[x]+=s[e[i].to],k[x]+=k[e[i].to];
}

int main()
{
    n=read();m=read();
    for (int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        addedge(x,y);
    }
    dfs1(1,0);
    if (odd!=even&&!(len&1)||(abs(odd-even)&1)&&(len&1)) {puts("-1");return 0;}
    for (int i=1;i<=n;i++) s[i]=(dep[i]&1)?1:-1;
    int ans=0;
    if (len&1) s[sx]+=(odd-even)/2,s[sy]+=(odd-even)/2,ans+=abs(odd-even)/2;
    else k[sx]=1,k[sy]=-1;
    memset(vis,0,sizeof(vis));
    dfs2(1);
    int tot=1;
    for (int i=1;i<=n;i++)
        if (!k[i]) ans+=abs(s[i]);
        else if (k[i]==1) a[++tot]=-s[i];
        else a[++tot]=s[i];
    sort(a+1,a+tot+1);
    int mid=a[(tot+1)/2];
    ans+=abs(mid);
    for (int i=1;i<=n;i++) if (k[i]) ans+=abs(k[i]*mid+s[i]);
    printf("%d",ans);
    return 0;
}