#include <bits/stdc++.h>
inline int gi() {
  int x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int f[100010];
int fir[100010], dis[200010], nxt[200010], id;
inline void link(int a, int b) { nxt[++id] = fir[a], fir[a] = id, dis[id] = b; }
int ans = 0;
inline void dfs(int x, int fa = -1, int fa2 = -1) {
  f[x] = 1;
  for (int i = fir[x]; i; i = nxt[i]) {
    if (fa == dis[i]) continue;
    if (fa2 == dis[i]) continue;
    dfs(dis[i], x);
    f[x] -= f[dis[i]];
  }
}
int stk[100010], tp;
int ins[100010];
int cir[200010], p;
inline void pre_dfs(int x, int lst = -1) {
  stk[++tp] = x;
  ins[x] = tp;
  for (int i = fir[x]; i; i = nxt[i]) {
    if (lst == dis[i]) continue;
    if (ins[dis[i]]) {
      for (int j = ins[dis[i]]; j <= ins[x]; ++j) cir[++p] = stk[j];
      return;
    }
    pre_dfs(dis[i], x);
    if (p) return;
  }
  --tp;
  ins[x] = 0;
}
int g[100010];
inline void DFS(int x, int fa = -1, int _f = 1) {
  f[x] = _f;
  if (x == cir[1])
    g[x] = 1;
  else if (x == cir[2])
    g[x] = -1;
  for (int i = fir[x]; i; i = nxt[i]) {
    if (x == cir[1] && dis[i] == cir[2]) continue;
    if (x == cir[2] && dis[i] == cir[1]) continue;
    if (dis[i] == fa) continue;
    DFS(dis[i], x, -_f);
    f[x] += f[dis[i]];
    g[x] += g[dis[i]];
  }
}
int s[100010];
int main() {
  int n = gi(), m = gi(), a, b;
  for (int i = 1; i <= m; ++i) a = gi(), b = gi(), link(a, b), link(b, a);
  if (m == n - 1) {
    dfs(1);
    for (int i = 1; i <= n; ++i) ans += abs(f[i]);
    if (f[1]) ans = -1;
    printf("%d\n", ans);
  } else {
    pre_dfs(1);
    for (int i = 1; i <= p; ++i) cir[i + p] = cir[i];
    cir[0] = cir[p];
    if (p % 2 == 0) {
      DFS(1);
      int o = 0;
      for (int i = 1; i <= n; ++i)
        if (g[i] == 0)
          ans += abs(f[i]);
        else if (g[i] > 0)
          s[++o] = f[i];
        else
          s[++o] = -f[i];
      s[++o] = 0;
      std::sort(s + 1, s + o + 1);
      for (int i = 1; i <= o; ++i) ans += abs(s[(1 + o) / 2] - s[i]);
    } else {
      for (int i = 1; i <= p; ++i) dfs(cir[i], cir[i - 1], cir[i + 1]);
      for (int i = 1; i <= n; ++i) ans += abs(f[i]);
      for (int i = 1; i <= p; ++i) ans -= abs(f[cir[i]]);
      for (int i = 1, t = -1; i < p; ++i, t = -t) f[cir[p]] += t * f[cir[i]];
      if (f[cir[p]] % 2) return puts("-1"), 0;
      f[cir[p]] /= 2;
      for (int i = p - 1; i; --i) f[cir[i]] -= f[cir[i + 1]];
      for (int i = 1; i <= p; ++i) ans += abs(f[cir[i]]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
