#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#define il inline
#define ri register
using namespace std;
struct point{
    point*next;int to;
}*t,*head[100001];
int ans;
int dep[100001],s[100001],
    u,v,rl,check,te[100001],
    round[100001],rt;
il int Abs(int);
il void read(int&),link(int,int);
void dfs1(int,int),dfs2(int,int),dfs3(int,int);
int main(){
    int n,m,i,j,k;read(n),read(m);
    for(i=1;i<=m;++i)read(j),read(k),link(j,k);
    s[0]=-1,dfs1(1,0);
    if(m==n-1){
        if(check)puts("-1"),exit(0);
        dfs2(1,0),printf("%d",ans);return 0;
    }
    else if(rl&1){
        if(check&1)puts("-1"),exit(0);
        check/=2,ans+=Abs(check);
        s[u]-=check,s[v]-=check,dfs2(1,0);
        printf("%d",ans);return 0;
    }
    else{
        if(check)puts("-1"),exit(0);
        te[u]=1,te[v]=-1,dfs3(1,0);
        round[++rt]=0,sort(round+1,round+rt+1);
        j=round[rt+1>>1];
        for(i=1;i<=rt;++i)ans+=Abs(round[i]-j);
        printf("%d",ans);
    }
    return 0;
}
void dfs3(int x,int pa){
    for(point*i(head[x]);i!=NULL;i=i->next)
        if(i->to!=pa)
            dfs3(i->to,x),s[x]+=s[i->to],
                te[x]+=te[i->to];
    if(te[x])round[++rt]=te[x]*s[x];
    else ans+=Abs(s[x]);
}
void dfs2(int x,int pa){
    for(point*i(head[x]);i!=NULL;i=i->next)
        if(i->to!=pa)
            dfs2(i->to,x),s[x]+=s[i->to];
    ans+=Abs(s[x]);
}
void dfs1(int x,int pa){
    dep[x]=dep[pa]+1,s[x]=~s[pa]+1,check+=s[x];
    for(point*i(head[x]);i!=NULL;i=i->next)
        if(i->to!=pa){
            if(dep[i->to])
                u=x,v=i->to,rl=Abs(dep[x]-dep[i->to])+1,
                    i->to=pa;
            else dfs1(i->to,x);
        }
}
il int Abs(int x){
    return x<0?~x+1:x;
}
il void link(int i,int j){
    t=new point,t->next=head[i],head[i]=t,t->to=j;
    t=new point,t->next=head[j],head[j]=t,t->to=i;
}
il void read(int&x){
    x&=0;ri char c;while(c=getchar(),c<'0'||c>'9');
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}
