#include <bits/stdc++.h>
using namespace std;
struct E {
  int to, nxt;
  bool canbe;
} ed[100005 << 1];
int a, b, ans, top, n, m, i, j, lca, u, v, sum, num;
int dep[100005], head[100005], val[100005], s[100005], fa[100005], k[100005],
    q[100005];
void read(int &p) {
  p = 0;
  char x = getchar();
  while (x < '0' || x > '9') x = getchar();
  while (x >= '0' && x <= '9') {
    p = p * 10 + x - '0';
    x = getchar();
  }
}
void add(int u, int v) {
  top++;
  ed[top] = (E){v, head[u]};
  head[u] = top;
  ed[top].canbe = true;
}
void dfs(int x) {
  int i;
  for (i = head[x]; i; i = ed[i].nxt)
    if (!dep[ed[i].to]) {
      dep[ed[i].to] = dep[x] + 1;
      fa[ed[i].to] = x;
      dfs(ed[i].to);
    } else if (fa[x] != ed[i].to) {
      ed[i].canbe = false;
      a = ed[i].to;
      b = x;
    }
}
void solve(int x) {
  int i;
  s[x] = val[x];
  for (i = head[x]; i; i = ed[i].nxt)
    if ((ed[i].to != fa[x]) && (ed[i].canbe)) {
      solve(ed[i].to);
      s[x] += s[ed[i].to];
    }
}
int main() {
  read(n);
  read(m);
  for (i = 1; i <= m; ++i) {
    read(a);
    read(b);
    add(a, b);
    add(b, a);
  }
  dep[1] = 1;
  a = b = 0;
  dfs(1);
  for (i = 1; i <= n; ++i)
    if (dep[i] & 1)
      num++, val[i] = 1;
    else
      num--, val[i] = -1;
  if (n == m - 1) {
    if (num) {
      puts("-1");
      return 0;
    }
    solve(1);
    for (i = 1; i <= n; ++i) ans += abs(s[i]);
    printf("%d\n", ans);
    return 0;
  }
  u = a;
  v = b;
  if (dep[a] < dep[b]) swap(a, b);
  while (dep[a] > dep[b]) sum++, a = fa[a];
  while (a != b) {
    a = fa[a];
    b = fa[b];
  }
  lca = a;
  if (!(sum & 1)) {
    if (num & 1) {
      puts("-1");
      return 0;
    }
    if (num > 0) {
      val[u] -= (num >> 1);
      val[v] -= (num >> 1);
    }
    if (num < 0) {
      val[u] += ((-num) >> 1);
      val[v] += ((-num) >> 1);
    }
    ans += (abs(num) >> 1);
    solve(1);
    for (i = 1; i <= n; ++i) ans += abs(s[i]);
    printf("%d\n", ans);
    return 0;
  }
  if (num) {
    puts("-1");
    return 0;
  }
  a = u;
  b = v;
  top = 1;
  q[1] = 0;
  solve(1);
  for (i = 1; i <= n; ++i) ans += abs(s[i]);
  while (a != lca) {
    top++;
    q[top] = (-s[a]);
    ans -= abs(s[a]);
    a = fa[a];
  }
  while (b != lca) {
    top++;
    q[top] = s[b];
    ans -= abs(s[b]);
    b = fa[b];
  }
  sort(q + 1, q + 1 + top);
  num = q[((top + 1) >> 1)];
  for (i = 1; i <= top; ++i) ans += abs(q[i] - num);
  printf("%d\n", ans);
  return 0;
}
