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

using namespace std;

#define REP(i,a,b) for(int i=(a),_ed=(b);i<=_ed;++i)

#define DREP(i,a,b) for(int i=(a),_ed=(b);i>=_ed;--i)

#define pb(x) push_back((x))

typedef long long ll;

inline int read(){

    register int x=0,f=1;register char ch=getchar();

    while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}

    while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}

    return f?x:-x;

}



const int N=1e5+5;

int n,m,dep[N],s,t,flg,S[N],k[N],stk[N],tp;

ll ans;

vector<int> E[N];



inline void Dfs(int u,int pa){

    for(int v:E[u]){

	if(v==pa)continue;

	if(dep[v]){flg=(dep[u]==dep[v]);s=u,t=v;continue;}

	dep[v]=dep[u]*-1;Dfs(v,u);

    }

}

inline void Cal(int u,int pa){

    for(int v:E[u]){

	if(v==pa||(u==s&&v==t)||(v==s&&u==t))continue;

	Cal(v,u);

	S[u]+=S[v];k[u]+=k[v];

    }

}



int main(){

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

    n=read(),m=read();

    REP(i,1,m){int u=read(),v=read();E[u].pb(v),E[v].pb(u);}

    dep[1]=1;Dfs(1,0);

    int sum=0;

    REP(i,1,n)sum+=(S[i]=dep[i]);

    if(m==n-1){if(sum)return puts("-1"),0;}

    else if((flg&&(sum&1))||(!flg&&sum))return puts("-1"),0;

    if(m==n){

	if(flg){

	    S[s]-=sum/2,S[t]-=sum/2;

	    ans+=abs(sum/2);

	}

	else k[s]=1,k[t]=-1;

    }

    Cal(1,0);

    REP(i,1,n)

	if(!k[i])ans+=abs(S[i]);

	else if(~k[i])stk[++tp]=-S[i];

	else stk[++tp]=S[i];

    stk[++tp]=0;

    sort(stk+1,stk+tp+1);

    int x=stk[(tp+1)>>1];

    REP(i,1,tp)ans+=abs(stk[i]-x);

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

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.5429833920664717x):
#include<algorithm>

#include<iostream>

#include<cstring>

#include<cstdio>

using namespace std;

const int maxn = 100000 + 10;

 

inline int rd() {

  int x = 0; char ch = getchar();

  while(ch < '0' || ch > '9') ch = getchar();

  do{

    x = (x << 1) + (x << 3) + ch - '0';

    ch = getchar();

  }while(ch >= '0' && ch <= '9');

  return x;

}

 

int n, m, cnt, h[maxn], sum = 0, flag, kx[maxn], val[maxn];

 

struct enode{

  int v, n;

  enode() {}

  enode(int _v, int _n):v(_v), n(_n) {}

}e[maxn << 1];

 

inline void addedge(int u, int v) { cnt ++; e[cnt] = enode(v,h[u]); h[u] = cnt; }

 

int dep[maxn], vis[maxn], Sx, Tx, st[maxn];

void dfs(int u, int fa) {

  dep[u] = dep[fa] ^ 1;

  vis[u] = 1;

  for(int i = h[u];~ i;i = e[i].n) {

    int v = e[i].v;

    if(v == fa) continue;

    if(vis[v]) Sx = u, Tx = v, flag = (dep[u] == dep[v]);

    else dfs(v,u);

  }

}

 

void solve(int u, int fa) {

  for(int i = h[u];~ i;i = e[i].n) {

    int v = e[i].v;

    if(v == fa) continue;

    if((u == Sx && v == Tx) || (u == Tx && v == Sx)) continue;

    solve(v,u);

    val[u] += val[v];

    kx[u] += kx[v];

    }

}

 

int main() {

  n = rd(); m = rd();

  cnt = 0;

  memset(h,-1,sizeof(h));

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

    int u, v;

    u = rd(); v = rd();

    addedge(u,v);

    addedge(v,u);

  }

  flag = 0;

  dep[0] = 0;

  dfs(1,0);

  int ans = 0;

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

    if(dep[i]) val[i] = 1;

    else val[i] = -1;

    sum += val[i];

  }

  if(m == n - 1) {

    if(sum) {

      puts("-1");

      return 0;

      }

  }

  else {

    if(flag) {

      if(sum & 1) {

        puts("-1");

        return 0;

        }

        val[Sx] -= sum / 2;

        val[Tx] -= sum / 2;

        ans += abs(sum) / 2;

      }

      else {

        if(sum) {

          puts("-1");

          return 0;

        }

        kx[Sx] = 1;

        kx[Tx] = -1;

    }

  }

  solve(1,0);

  int top;

  st[top = 1] = 0;

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

    if(!kx[i]) ans += abs(val[i]);

    else st[++ top] = - val[i];

    }

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

    int k = st[(top + 1) >> 1];

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

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

    }

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

  return 0;

}