#include <bits/stdc++.h>
inline int in() {
  int k = 0;
  char ch = getchar();
  bool p = 1;
  while (ch < '-') ch = getchar();
  if (ch == '-') ch = getchar(), p = 0;
  while (ch > '-') k = k * 10 + ch - '0', ch = getchar();
  return p ? k : -k;
}
const int N = 1e5 + 5;
int fa[N], head[N], to[N << 1], nxt[N << 1], cnt, n, m;
inline void add(int u, int v) {
  to[++cnt] = v, nxt[cnt] = head[u], head[u] = cnt;
  to[++cnt] = u, nxt[cnt] = head[v], head[v] = cnt;
}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline int abs(int x) { return x < 0 ? -x : x; }
namespace w1 {
int f[N];
long long ans;
void dfs(int u, int pa = 0, int d = 1) {
  f[u] = d & 1 ? 1 : -1;
  for (int i = head[u]; i; i = nxt[i])
    if (to[i] != pa) dfs(to[i], u, d + 1), f[u] += f[to[i]];
  if (u != 1) ans += abs(f[u]);
}
int work() {
  for (int i = 2; i <= n; ++i) add(in(), in());
  ans = 0;
  dfs(1);
  if (f[1])
    puts("-1");
  else
    printf("%lld\n", ans);
  return 0;
}
}  // namespace w1
int f[N], dep[N], st[N], tot;
void dfs(int u, int pa = 0, int d = 1) {
  f[u] = d & 1 ? 1 : -1;
  dep[u] = d, fa[u] = pa;
  for (int i = head[u]; i; i = nxt[i])
    if (to[i] != pa) dfs(to[i], u, d + 1), f[u] += f[to[i]];
}
int main() {
  n = in(), m = in();
  if (n & 1) return puts("-1"), 0;
  if (m == n - 1) return w1::work();
  int Eu, Ev;
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= n; ++i) {
    int u = in(), v = in();
    if (find(u) == find(v)) {
      Eu = u, Ev = v;
      continue;
    }
    fa[find(u)] = find(v);
    add(u, v);
  }
  dfs(Eu);
  if (dep[Ev] & 1) {
    long long ans = 0;
    f[Ev] -= f[Eu] >> 1;
    ans = -abs(f[Eu]);
    for (int i = 1; i <= n; ++i) ans += abs(f[i]);
    printf("%lld\n", ans);
  } else {
    if (f[Eu]) return puts("-1"), 0;
    int u = Ev;
    long long ans = 0;
    while (u != Eu) st[++tot] = f[u], u = fa[u];
    std::sort(st + 1, st + tot + 1);
    int x = st[tot >> 1];
    u = Ev;
    while (u) f[u] -= x, u = fa[u];
    for (int i = 1; i <= n; ++i) ans += abs(f[i]);
    printf("%lld\n", ans);
  }
  return 0;
}
