// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

vector<int> G[N];

int delv, delu;

int n, m, x, y, d[N], fa[N], dp[N], q[N];

void dfs(int x, int fx) {

    d[x] = d[fx] + 1;

    fa[x] = fx;

    dp[x] = d[x] % 2 ? 1 : -1;

    for (int y : G[x]) {

        if (y == fx) continue;

        if (d[y]) {

            delv = y;

            delu = x;

            continue;

        }

        dfs(y, x);

        dp[x] += dp[y];

    }

}

void SolveTree() {

    if (dp[1]) {

        puts("-1");

        return;

    }

    int ans = 0;

    for (int i = 1; i <= n; i++) ans += abs(dp[i]);

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

}

void SolveOdd() {

    if (dp[1] % 2) {

        puts("-1");

        return;

    }

    int ans = 0, t = -dp[1] / 2;

    ans = abs(t);

    for (; delu; delu = fa[delu]) dp[delu] += t;

    for (; delv; delv = fa[delv]) dp[delv] += t;

    for (int i = 1; i <= n; i++) ans += abs(dp[i]);

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

}

void SolveEven() {

    if (dp[1]) {

        puts("-1");

        return;

    }

    int ans, top = 0, mid;

    for (; delv != delu; delv = fa[delv]) {

        q[++top] = dp[delv];

        d[delv] = 0;

    }

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

    mid = q[(top + 1) / 2];

    ans = abs(mid);

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

        if (d[i]) ans += abs(dp[i]);

    for (int i = 1; i <= top; i++) ans += abs(q[i] - mid);

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

}

int main() {

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

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

        scanf("%d%d", &x, &y);

        G[x].push_back(y);

        G[y].push_back(x);

    }

    dfs(1, 0);

    if (m == n - 1)

        SolveTree();

    else if ((d[delv] - d[delu]) % 2)

        SolveEven();

    else

        SolveOdd(); 

}

// Faster C++ solution for the same problem (measured speedup: 3.5797638289863034x):
//code from https://www.luogu.com.cn/blog/flashblog/solution-at2046

#include<bits/stdc++.h>

#define R register int

#define G if(++ip==ie)if(fread(ip=buf,1,SZ,stdin))

using namespace std;

const int SZ=1<<19,N=1e5+9,M=2*N;

char buf[SZ],*ie=buf+SZ,*ip=ie-1;

inline int in(){

    G;while(*ip<'-')G;

    R x=*ip&15;G;

    while(*ip>'-'){x*=10;x+=*ip&15;G;}

    return x;

}

int he[N],ne[M],to[M],f[N],a[N],b[N];

int Getf(R x){

    return x==f[x]?x:f[x]=Getf(f[x]);

}

void Dfs(R x,R op){

    a[x]=op;

    for(R y,i=he[x];i;i=ne[i])

        if((y=to[i])!=f[x])

            f[y]=x,Dfs(y,-op),a[x]+=a[y];

}

int main(){

    R n=in(),m=in(),rt=1,re=1,p=0,ans=0;

    for(R i=1;i<=n;++i)f[i]=i;

    for(R p=0,i=1;i<=m;++i){

        R x=in(),y=in();

        if(Getf(x)!=Getf(y))f[f[x]]=f[y];

        else{rt=x,re=y;continue;}//把多的边单独拿出来

        ne[++p]=he[x];to[he[x]=p]=y;

        ne[++p]=he[y];to[he[y]=p]=x;

    }

    f[rt]=0;Dfs(rt,1);

    if(n==m){//基环树

        for(R x=re;x;x=f[x])b[++p]=a[x];

        if(p&1){//奇环

            if(a[rt]&1)return puts("-1"),0;

            for(R x=re;x;x=f[x])a[x]-=a[rt]>>1;

        }

        else{//偶环

            if(a[rt])return puts("-1"),0;

            sort(b+1,b+p+1);

            for(R x=re;x;x=f[x])a[x]-=b[p>>1];

        }

    }//树

    else if(a[rt])return puts("-1"),0;

    for(R i=1;i<=n;++i)ans+=abs(a[i]);

    return cout<<ans<<endl,0;

}