#include <bits/stdc++.h>
using namespace std;
const int SZ = 1 << 19, N = 1e5 + 9, M = 2 * N;
char buf[SZ], *ie = buf + SZ, *ip = ie - 1;
inline int in() {
  if (++ip == ie)
    if (fread(ip = buf, 1, SZ, stdin))
      ;
  while (*ip < '-')
    if (++ip == ie)
      if (fread(ip = buf, 1, SZ, stdin))
        ;
  int x = *ip & 15;
  if (++ip == ie)
    if (fread(ip = buf, 1, SZ, stdin))
      ;
  while (*ip > '-') {
    x *= 10;
    x += *ip & 15;
    if (++ip == ie)
      if (fread(ip = buf, 1, SZ, stdin))
        ;
  }
  return x;
}
int h[N], w[M], o, q[M], f[N], c[N], v[N];
int Getf(int x) { return x == f[x] ? x : f[x] = Getf(f[x]); }
void Dfs(int x, int op) {
  c[x] = op;
  for (int y, i = h[x]; i; i = w[i])
    if ((y = q[i]) != f[x]) f[y] = x, Dfs(y, -op), c[x] += c[y];
}
int main() {
  int n = in(), m = in(), qq = 1, ww = 1, ans = 0;
  for (int i = 1; i <= n; ++i) f[i] = i;
  for (int i = 1; i <= m; ++i) {
    int qw = in(), wq = in();
    if (Getf(qw) != Getf(wq))
      f[f[qw]] = f[wq];
    else {
      qq = qw, ww = wq;
      continue;
    }
    o++;
    w[o] = h[qw];
    q[o] = wq;
    h[qw] = o;
    w[o] = h[wq];
    q[o] = qw;
    h[wq] = o;
  }
  f[qq] = 0;
  Dfs(qq, 1);
  if (n == m) {
    long long u = 0;
    for (int i = ww; i; i = f[i]) {
      u++;
      v[u] = c[i];
    }
    if (u % 2) {
      if (c[qq] % 2) return puts("-1"), 0;
      for (int i = ww; i; i = f[i]) c[i] -= c[qq] / 2;
    } else {
      if (c[qq]) return puts("-1"), 0;
      sort(v + 1, v + u + 1);
      for (int i = ww; i; i = f[i]) c[i] -= v[u / 2];
    }
  } else if (c[qq])
    return puts("-1"), 0;
  for (int i = 1; i <= n; ++i) ans += abs(c[i]);
  return cout << ans, 0;
}
