// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

#include<cmath>

using namespace std;



typedef long long ll;

const ll MAXN=1e5+5;



ll n,m,cir,spx,spy;

ll dis[MAXN],sum[MAXN],nx[MAXN];

ll sta[MAXN],top;

bool vis[MAXN];

vector<ll> E[MAXN];



void dfs(ll u,ll fa=0){

    vis[u]=1;

    dis[u]=dis[fa]+1;

    for(ll i=0;i<(ll)E[u].size();i++){

        ll v=E[u][i];

        if(v==fa) continue;

        if(vis[v]){

            cir=dis[u]-dis[v]+1;

            spx=u,spy=v;

            continue;

        }

        dfs(v,u);

    }

}



void solve(ll u,ll fa=0){

    vis[u]=1;

    for(ll i=0;i<(ll)E[u].size();i++){

        ll v=E[u][i];

        if(v==fa||vis[v]) continue;

        solve(v,u);

        nx[u]+=nx[v];

        sum[u]+=sum[v];

    }

}



int main(){

    //freopen("print.in","r",stdin);

    //freopen("print.out","w",stdout);



    scanf("%lld%lld",&n,&m);

    for(ll i=1;i<=m;i++){

        ll u,v;

        scanf("%lld%lld",&u,&v);

        E[u].push_back(v);

        E[v].push_back(u);

    }

    dis[0]=-1;

    dfs(1);

    ll tot=0,ans=0;

    for(ll i=1;i<=n;i++)

        tot+=(sum[i]=(dis[i]&1?-1:1));

    if(!cir){ if(tot) return puts("-1"),0;}

    else{

        if(cir&1){

            if(tot&1) return puts("-1"),0;

            sum[spx]-=tot/2;

            sum[spy]-=tot/2;

            ans+=abs(tot)/2;

        }

        else{

            if(tot) return puts("-1"),0;

            nx[spx]=1;

            nx[spy]=-1;

        }

    }

    memset(vis,0,sizeof vis);

    solve(1);

    for(ll i=1;i<=n;i++){

        if(!nx[i]) ans+=abs(sum[i]);

        else{

            if(nx[i]==1) sum[i]*=-1;

            sta[++top]=sum[i];

        }

    }

    sta[++top]=0;

    sort(sta+1,sta+top+1);

    ll k=sta[(top+1)/2];

    for(ll i=1;i<=top;i++)

        ans+=abs(sta[i]-k);

    printf("%lld\n",ans);



    fclose(stdin);

    fclose(stdout);

}


// Faster C++ solution for the same problem (measured speedup: 3.509333424254161x):
#include<bits/stdc++.h>

using namespace std;

// #define int long long

void read(int &x)

{

    int r = 0, ne = 1;

    char c;

    while (c < '0' || c > '9')

    {

        if (c == '-')

            ne = -1;

        c = getchar();

    }

    while (c >= '0' && c <= '9')

    {

        r = r * 10 + c - '0';

        c = getchar();

    }

    x = r * ne;

}

struct  Edge

{

    /* data */

    int i,nxt;

};

/* 链表 */

int h[100002],tot;

Edge e[200002];

inline void _add(int x,int y){

    e[++tot].i=y;

    e[tot].nxt=h[x];

    h[x]=tot;

    e[++tot].i=x;

    e[tot].nxt=h[y];

    h[y]=tot;

}

/* 输入 */

int n,m,x,y;

/* 偶环-求解 */

int s[100002],top,mid;

/* dfs */

bool odd;/* 奇环 */

int A,B;/* 环两个连接点 */

int w[100002];/* 点绿色权值(1,-1) */ /* 第二遍扫时存的是子树总权值 */

int k[100002];/* 偶环时标记多余边到LCA的点 */

int sum;/* 权值总和 */

long long ans;/* 答案 */

void dfs(int x,int fa){/* 染色 */

    for(int i=h[x];i;i=e[i].nxt){      

        int o=e[i].i;

        if(o==fa)continue;

        if(w[o]){

            if(w[o]==w[x])

                odd=true;

            A=x,B=o;

        }else{

            w[o]=-w[x];

            // sum+=w[o];

            dfs(o,x);

        }

    }

}

void Dfs(int x,int fa){/* 求解 */

    for(int i=h[x];i!=0;i=e[i].nxt){

        int o=e[i].i;

        if(o==fa||(o==A&&x==B)||(o==B&&x==A))continue;/* 排除基环树的干扰 */

        Dfs(o,x);

        w[x]+=w[o];

        k[x]+=k[o];

    }

}

signed main(){

    read(n),read(m);

    for(register int i=0;i<m;++i){

        read(x),read(y);

        _add(x,y);

    }

    // if(n%2){

    //     puts("-1");

    //     return 0;

    // }

    w[1]=1;

    // sum=1;

    dfs(1,0);

    for(int i=1;i<=n;++i)sum+=w[i];    

    if(m+1==n)/* 树 */{

        if(sum){

            puts("-1");

           return 0;

        }

    }else{

        if(odd)/* 奇环 */{

            if(sum&1){

                puts("-1");

                return 0;

            }

            ans+=abs(sum>>1);

            w[A]-=sum>>1,w[B]-=sum>>1;

        }else/* 偶环 */{

            if(sum){

                puts("-1");

                return 0;

            }else 

                k[A]=1,k[B]=-1;

        }

    }

    Dfs(1,0);

    for(register int i=1;i<=n;++i){

        if(k[i])

            s[++top]=k[i]*w[i];

        else

            ans+=abs(w[i]);

    }

    s[++top]=0;

    sort(s+1,s+top+1);

    mid=s[(top+1)>>1];

    for(register int i=1;i<=top;++i)

        ans+=abs(s[i]-mid);

    printf("%lld\n",ans);

    return 0;

}