#include <bits/stdc++.h>
using namespace std;
int n, m;
long long ans;
int tt[200005], he[100005], nx[200005], _;
int b[100005], c[100005], X, Y, a[100005], f[100005], q[100005], Q;
void add(int x, int y) { nx[++_] = he[x], he[x] = _, tt[_] = y; }
int dfs(int x, int y, int z) {
  int s = z;
  for (int i = he[x]; i; i = nx[i])
    if (tt[i] != y) s += dfs(tt[i], x, -z);
  ans += abs(s);
  return s;
}
void ope(int x, int y) {
  c[x] = c[y] ^ 1;
  for (int i = he[x]; i; i = nx[i]) {
    if (tt[i] != y) {
      if (c[tt[i]] != -1)
        Y = x, X = tt[i];
      else
        ope(tt[i], x);
    } else
      f[x] = tt[i];
  }
}
void sol(int x) {
  c[x] = 1;
  for (int i = he[x]; i; i = nx[i])
    if (!c[tt[i]]) sol(tt[i]), a[x] += a[tt[i]];
  ans += abs(a[x]);
}
void sol2(int x) {
  c[x] = 1;
  for (int i = he[x]; i; i = nx[i])
    if (!c[tt[i]]) sol2(tt[i]), a[x] += a[tt[i]];
  if (!b[x])
    ans += abs(a[x]);
  else
    q[++Q] = a[x];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y), add(y, x);
  }
  if (m == n - 1) {
    if (dfs(1, 0, 1))
      puts("-1");
    else
      printf("%d\n", ans);
    return 0;
  }
  memset(c, -1, sizeof(c));
  c[0] = 0;
  ope(1, 0);
  if (c[X] == c[Y]) {
    if (n & 1) {
      puts("-1");
      return 0;
    }
    int cnt = 0;
    for (int i = 1; i <= n; ++i) {
      if (c[i] == c[X])
        --cnt, a[i] = 1;
      else
        ++cnt, a[i] = -1;
    }
    a[X] += cnt / 2, a[Y] += cnt / 2;
    memset(c, 0, sizeof(c));
    sol(1);
    printf("%lld\n", ans + abs(cnt / 2));
    return 0;
  }
  for (int i = X; i != Y; i = f[i]) b[i] = 1;
  for (int i = 1; i <= n; ++i) a[i] = c[i] * 2 - 1;
  memset(c, 0, sizeof(c));
  sol2(1);
  if (a[0]) {
    puts("-1");
    return 0;
  }
  q[++Q] = 0;
  sort(q + 1, q + Q + 1);
  int x = q[(Q + 1) / 2];
  for (int i = 1; i <= Q; ++i) ans += abs(q[i] - x);
  printf("%lld\n", ans);
  return 0;
}
