#include <bits/stdc++.h>
long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return f * x;
}
long long max(long long a, long long b) { return a > b ? a : b; }
long long min(long long a, long long b) { return a < b ? a : b; }
void umax(long long& a, long long t) {
  if (t > a) a = t;
}
void umin(long long& a, long long t) {
  if (t < a) a = t;
}
const long long INF = 1ll << 50;
struct edge {
  long long v, nxt;
} e[200011 << 1 | 1];
long long cnt = 1, last[200011];
void adde(long long u, long long v) {
  e[++cnt].v = v;
  e[cnt].nxt = last[u], last[u] = cnt;
}
long long f[200011], ans = 0, cut = 0;
bool ring[200011];
void dfs1(long long u, long long now_dep, long long fa) {
  f[u] = (now_dep & 1 ? 1 : -1);
  for (long long i = last[u]; i; i = e[i].nxt) {
    long long v = e[i].v;
    if (v == fa || i == cut || i == (cut ^ 1)) continue;
    dfs1(v, now_dep + 1, u);
    f[u] += f[v];
  }
  ans += abs(f[u]);
}
long long dfn[200011], tim = 0, s[200011], top = 0, a[200011], cnt_ring = 0;
void dfs2(long long u, long long fa = 0) {
  dfn[u] = ++tim;
  s[++top] = u;
  for (long long i = last[u]; i; i = e[i].nxt) {
    long long v = e[i].v;
    if (v == fa) continue;
    if (!dfn[v])
      dfs2(v, u);
    else {
      cut = i;
      while (s[top] != v) ring[s[top--]] = 1;
      ring[v] = 1;
    }
    if (cut) return;
  }
  --top;
}
bool cmp(long long x, long long y) { return f[x] < f[y]; }
int main() {
  long long n = read(), m = read();
  for (long long i = 1; i <= m; ++i) {
    long long u = read(), v = read();
    adde(u, v), adde(v, u);
  }
  if (m == n - 1) {
    dfs1(1, 1, 0);
    printf("%lld", f[1] ? -1ll : ans);
    return 0;
  } else {
    dfs2(1);
    long long rt = e[cut].v;
    dfs1(rt, 1, 0);
    for (long long i = 1; i <= n; ++i)
      if (ring[i]) a[++cnt_ring] = i;
    if (cnt_ring & 1)
      printf("%lld", f[rt] & 1 ? -1ll : ans - f[rt] / 2);
    else {
      if (f[rt] & 1) return puts("-1"), 0;
      std::sort(a + 1, a + cnt_ring + 1, cmp);
      long long mid = f[a[cnt_ring >> 1]], ans = 0;
      for (long long i = 1; i <= cnt_ring; ++i) ans += abs(f[a[i]] - mid);
      printf("%lld", ans);
    }
  }
  return 0;
}
