#include <bits/stdc++.h>
using namespace std;
struct edge {
  int u, v, f, next;
};
struct edge e[200010];
struct unit {
  int x, y;
};
struct unit f[100010];
int n, m, num, p, S, T, cnt0, cnt1, tot;
int head[100010], c[100010], used[100010], b[100010];
long long ans;
void add(int u, int v) {
  e[num].u = u;
  e[num].v = v;
  e[num].next = head[u];
  head[u] = num;
  num++;
  e[num].u = v;
  e[num].v = u;
  e[num].next = head[v];
  head[v] = num;
  num++;
}
void dfs0(int i, int pre, int flag) {
  int j;
  used[i] = 1;
  c[i] = flag;
  f[i].y = flag ? 1 : -1;
  if (flag)
    cnt1++;
  else
    cnt0++;
  j = head[i];
  while (j != -1) {
    if (e[j].v != pre) {
      if (used[e[j].v]) {
        p = j;
        S = i;
        T = e[j].v;
      } else
        dfs0(e[j].v, i, flag ^ 1);
    }
    j = e[j].next;
  }
}
void dfs(int i, int fa) {
  int j;
  j = head[i];
  while (j != -1) {
    if (e[j].f == 0 && e[j].v != fa) {
      dfs(e[j].v, i);
      f[i].x += f[e[j].v].x;
      f[i].y += f[e[j].v].y;
    }
    j = e[j].next;
  }
}
int main() {
  int i, j, x, y;
  scanf("%d%d", &n, &m);
  memset(head, -1, sizeof(head));
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
  }
  dfs0(1, 0, 0);
  if (m == n - 1) {
    if (cnt0 != cnt1) {
      printf("%d\n", -1);
      return 0;
    }
    dfs(1, 0);
    for (i = 1; i <= n; i++) ans += abs(f[i].y);
    printf("%lld\n", ans);
  } else if (c[S] == c[T]) {
    if (n & 1) {
      printf("%d\n", -1);
      return 0;
    }
    f[S].y += (cnt0 - cnt1) / 2;
    f[T].y += (cnt0 - cnt1) / 2;
    ans += abs((cnt0 - cnt1) / 2);
    e[p].f = e[p ^ 1].f = 1;
    dfs(1, 0);
    for (i = 1; i <= n; i++) ans += abs(f[i].y);
    printf("%lld\n", ans);
  } else {
    f[S].x = 1;
    f[T].x = -1;
    e[p].f = e[p ^ 1].f = 1;
    dfs(1, 0);
    b[++tot] = 0;
    for (i = 1; i <= n; i++)
      if (f[i].x == 0)
        ans += abs(f[i].y);
      else if (f[i].x == 1)
        b[++tot] = -f[i].y;
      else
        b[++tot] = f[i].y;
    sort(b + 1, b + tot + 1);
    for (i = 1; i <= tot; i++) ans += abs(b[(tot + 1) / 2] - b[i]);
    printf("%lld\n", ans);
  }
  return 0;
}
