#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define DCSB {printf("-1");exit(0);}
using namespace std;
struct edge{
    int v,next;
}a[500001];
int n,m,u,v,ans=0,els,huanu,huanv,tot=0,q[500001],head[500001],fa[500001],dep[500001],f[500001];
void add(int u,int v){
    a[++tot].v=v;
    a[tot].next=head[u];
    head[u]=tot;
}
void dfs(int u,int ff,int dpt){
    dep[u]=dpt;
    fa[u]=ff;
    f[u]=(dpt%2)?1:-1;
    for(int tmp=head[u];tmp!=-1;tmp=a[tmp].next){
        int v=a[tmp].v;
        if(v!=ff){
            if(dep[v]){
                huanu=u;
                huanv=v;
                continue;
            }
            dfs(v,u,dpt+1);
            f[u]+=f[v];
        }
    }
}
void gao_tree(){
    if(f[1]!=0)DCSB
    for(int i=1;i<=n;i++)ans+=abs(f[i]);
    printf("%d",ans);
}
void gao_jihuan(){
    if(f[1]%2!=0)DCSB
    els=-f[1]/2;
    ans=abs(els);
    for(;huanu;huanu=fa[huanu])f[huanu]+=els;
    for(;huanv;huanv=fa[huanv])f[huanv]+=els;
    for(int i=1;i<=n;i++)ans+=abs(f[i]);
    printf("%d",ans);
}
void gao_ouhuan(){
    int top,mid;
    top=0;
    if(f[1])DCSB
    for(;huanv!=huanu;huanv=fa[huanv]){
        q[++top]=f[huanv];
        dep[huanv]=0;
    }
    sort(q+1,q+top+1);
    mid=q[(top+1)/2];
    ans=abs(mid);
    for(int i=1;i<=n;i++)if(dep[i])ans+=abs(f[i]);
    for(int i=1;i<=top;i++)ans+=abs(q[i]-mid);
    printf("%d",ans);
}
int main(){
    memset(head,-1,sizeof(head));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0,1);
    if(m==n-1)gao_tree();
    else if((dep[huanv]-dep[huanu])%2==0)gao_jihuan();
    else gao_ouhuan();
    return 0;
}