#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct node {
  int y, next;
} a[N << 1];
long long an;
int S[N], fa[N], h[N], head[N], s[N], k[N], i, j, n, m, x, y, z, g;
char c;
inline char getc() {
  static char s[10000000], *l = s, *r = s;
  if (l == r) l = s, r = s + fread(s, 1, 10000000, stdin);
  return l == r ? 0 : *l++;
}
int read() {
  int z = 0;
  do c = getc();
  while (c < '0' || c > '9');
  while (c >= '0' && c <= '9') z *= 10, z += c - '0', c = getc();
  return z;
}
void jb(int x, int y) {
  a[++g] = (node){y, head[x]}, head[x] = g, a[++g] = (node){x, head[y]},
  head[y] = g;
}
void dfs(int w) {
  h[w] = h[fa[w]] + 1;
  s[w] = h[w] & 1 ? 1 : -1;
  for (register int j = head[w]; j; j = a[j].next) {
    int c = a[j].y;
    if (c != fa[w])
      if (h[c])
        x = w, y = c;
      else
        fa[c] = w, dfs(c), s[w] += s[c];
  }
}
int main() {
  n = read(), m = read();
  while (m--) jb(read(), read());
  dfs(1);
  if (m == n - 1) {
    if (s[1])
      an = -1;
    else
      for (i = 2; i <= n; ++i) an += abs(s[i]);
  } else if (h[x] - h[y] & 1) {
    S[g = 1] = 0;
    for (; x; x = fa[x]) ++k[x];
    for (; y; y = fa[y]) --k[y];
    for (i = 1; i <= n; ++i)
      if (k[i])
        S[++g] = k[i] * s[i];
      else
        an += abs(s[i]);
    sort(S + 1, S + g + 1);
    z = S[g + 1 >> 1];
    for (i = 1; i <= g; ++i) an += abs(z - S[i]);
  } else if (abs(s[1]) & 1)
    an = -1;
  else {
    an = abs(z = s[1] / 2);
    for (; x; x = fa[x]) s[x] -= z;
    for (; y; y = fa[y]) s[y] -= z;
    for (i = 2; i <= n; ++i) an += abs(s[i]);
  }
  printf("%lld\n", an);
  return 0;
}
