#include <bits/stdc++.h>
int n, m, t, ciru, cirv, ans, tp, flag;
struct node {
  int v, nex;
} e[100010 << 1];
typedef int arr[100010];
arr num, dep, head, fa, s;
void add(int x, int y) {
  e[++t].v = y, e[t].nex = head[x], head[x] = t;
  e[++t].v = x, e[t].nex = head[y], head[y] = t;
}
void dfs(int u) {
  num[u] = ((dep[u] = dep[fa[u]] + 1) & 1) ? 1 : -1;
  for (register int i = head[u], v = e[i].v; i; i = e[i].nex, v = e[i].v) {
    if (v ^ fa[u]) {
      if (dep[v]) {
        if (!flag) ciru = u, cirv = v, flag = 1;
      } else
        fa[v] = u, dfs(v), num[u] += num[v];
    }
  }
}
void solve_odd() {
  if (num[1] & 1) {
    ans = -1;
    return;
  }
  int fix = -num[1] / 2, x;
  ans = abs(fix);
  for (x = ciru; x; x = fa[x]) num[x] += fix;
  for (x = cirv; x; x = fa[x]) num[x] += fix;
  for (register int i = (1); i <= (n); i = -(~(i))) ans += abs(num[i]);
}
void solve_even() {
  if (num[1]) {
    ans = -1;
    return;
  }
  for (int x = ciru; x ^ cirv; x = fa[x]) s[++tp] = num[x], dep[x] = 0;
  int mid = (tp + 1) >> 1;
  std::nth_element(s + 1, s + mid + 1, s + tp + 1);
  ans = abs(s[mid]);
  for (register int i = (1); i <= (n); i = -(~(i)))
    if (dep[i]) ans += abs(num[i]);
  for (register int i = (1); i <= (tp); i = -(~(i))) ans += abs(s[i] - s[mid]);
}
int main() {
  int x, y;
  scanf("%d%d", &n, &m);
  for (register int i = (1); i <= (m); i = -(~(i)))
    scanf("%d%d", &x, &y), add(x, y);
  dfs(1);
  if (m == n - 1)
    if (num[1])
      ans = -1;
    else
      for (register int i = (1); i <= (n); i = -(~(i))) ans += abs(num[i]);
  else if ((dep[ciru] - dep[cirv] + 1) & 1)
    solve_odd();
  else
    solve_even();
  return printf("%d\n", ans), 0;
}
