#include <bits/stdc++.h>
using namespace std;
long long a, b, qw, wq, qq = 1, ww = 1, f[500001], o, h[500001], c[500001],
                        v[500001];
struct p {
  long long q, w;
} l[500001];
long long get(long long y) {
  if (f[y] == y) return y;
  return f[y] = get(f[y]);
}
void dfs(long long n, long long m) {
  c[n] = m;
  for (int i = h[n]; i; i = l[i].w) {
    if (l[i].q != f[n]) {
      f[l[i].q] = n;
      dfs(l[i].q, -m);
      c[n] += c[l[i].q];
    }
  }
}
int main() {
  for (int i = 1; i <= 500000; i++) {
    f[i] = i;
  }
  cin >> a >> b;
  for (int i = 1; i <= b; i++) {
    cin >> qw >> wq;
    if (get(qw) != get(wq)) {
      f[f[qw]] = f[wq];
    } else {
      qq = qw;
      ww = wq;
      continue;
    }
    o++;
    l[o].q = wq;
    l[o].w = h[qw];
    h[qw] = o;
    o++;
    l[o].q = qw;
    l[o].w = h[wq];
    h[wq] = o;
  }
  f[qq] = 0;
  dfs(qq, 1);
  if (a == b) {
    long long u = 0;
    for (int i = ww; i; i = f[i]) {
      u++;
      v[u] = c[i];
    }
    if (u % 2) {
      if (c[qq] % 2 == 1) {
        cout << "-1";
        return 0;
      }
      for (int i = ww; i; i = f[i]) {
        c[i] -= c[qq] / 2;
      }
    } else {
      if (c[qq] != 0) {
        cout << "-1";
        return 0;
      }
      sort(v + 1, v + u + 1);
      for (int i = ww; i; i = f[i]) {
        c[i] -= v[u / 2];
      }
    }
  } else {
    if (c[qq] != 0) {
      cout << "-1";
      return 0;
    }
  }
  long long ans = 0;
  for (int i = 1; i <= a; i++) {
    ans += abs(c[i]);
  }
  cout << ans;
  return 0;
}
