#include <bits/stdc++.h>
using namespace std;
void read(long long &x) {
  long long r = 0, ne = 1;
  char c;
  while (c < '0' || c > '9') {
    if (c == '-') ne = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = r * 10 + c - '0';
    c = getchar();
  }
  x = r * ne;
}
struct Edge {
  long long i, nxt;
};
long long h[100002], tot = 1;
Edge e[200002];
inline void _add(long long x, long long y) {
  e[tot].i = y;
  e[tot].nxt = h[x];
  h[x] = tot++;
  e[tot].i = x;
  e[tot].nxt = h[y];
  h[y] = tot++;
}
long long n, m, x, y;
long long s[100002], top, mid;
bool odd;
long long A, B;
long long w[100002];
long long k[100002];
long long sum;
long long ans;
inline void dfs(long long x, long long fa) {
  for (long long i = h[x]; i != 0; i = e[i].nxt) {
    long long o = e[i].i;
    if (o == fa) continue;
    if (w[o]) {
      if (w[o] == w[x]) odd = true;
      A = x, B = o;
    } else {
      w[o] = -w[x];
      sum += w[o];
      dfs(o, x);
    }
  }
}
inline void Dfs(long long x, long long fa) {
  for (long long i = h[x]; i != 0; i = e[i].nxt) {
    long long o = e[i].i;
    if (o == fa || (o == A && x == B) || (o == B && x == A)) continue;
    Dfs(o, x);
    w[x] += w[o];
    k[i] += k[o];
  }
}
signed main() {
  read(n), read(m);
  for (register long long i = 0; i < m; ++i) {
    read(x), read(y);
    _add(x, y);
  }
  w[1] = 1;
  sum = 1;
  dfs(1, 0);
  if (m != n) {
    if (sum) {
      puts("-1");
      return 0;
    }
  } else {
    if (odd) {
      if (sum & 1) {
        puts("-1");
        return 0;
      }
      ans += abs(sum / 2);
      w[A] -= sum / 2, w[B] -= sum / 2;
    } else {
      if (sum) {
        puts("-1");
        return 0;
      } else
        k[A] = 1, k[B] = -1;
    }
  }
  Dfs(1, 0);
  for (register long long i = 1; i <= n; ++i) {
    if (k[i])
      s[++top] = k[i] * w[i];
    else
      ans = ans + abs(w[i]);
  }
  s[++top] = 0;
  sort(s + 1, s + top + 1);
  mid = s[(top + 1) / 2];
  for (register long long i = 1; i <= top; ++i) ans += abs(s[i] - mid);
  printf("%lld\n", ans);
  return 0;
}
