#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, nxxt;
} e[200002];
int n, m, head[200002], cnt = 1, dep[200002], siz[200002], sx, sy, leh,
                        fa[200002], col[200002];
int sz[200002][2], b[200002], tot;
inline void ins(int u, int v) {
  e[cnt].to = v;
  e[cnt].nxxt = head[u];
  head[u] = cnt++;
}
void df5(int te, int la) {
  siz[te] = dep[te];
  for (int i = head[te]; i; i = e[i].nxxt) {
    int j = e[i].to;
    if (j == la) continue;
    dep[j] = -dep[te];
    df5(j, te);
    siz[te] += siz[j];
  }
}
void dfs(int te, int la) {
  siz[te] = col[te];
  for (int i = head[te]; i; i = e[i].nxxt) {
    int j = e[i].to;
    if (j == la) continue;
    if (dep[j] != -1) {
      sx = te, sy = j;
      continue;
    }
    dep[j] = dep[te] + 1;
    fa[j] = te;
    col[j] = -col[te];
    dfs(j, te);
    siz[te] += siz[j];
  }
}
void clfs(int te, int la) {
  for (int i = head[te]; i; i = e[i].nxxt) {
    int j = e[i].to;
    if (j == la || (sx == te && sy == j) || (sx == j && sy == te)) continue;
    clfs(j, te);
    sz[te][0] += sz[j][0], sz[te][1] += sz[j][1];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  if (m == n - 1) {
    for (int i = 1; i < n; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      ins(x, y);
      ins(y, x);
    }
    dep[1] = 1;
    df5(1, 1);
    long long ans = 0;
    for (int i = 2; i <= n; i++) ans += abs(siz[i]);
    if (siz[1]) ans = -1;
    printf("%lld\n", ans);
    return 0;
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  memset(dep, -1, sizeof(dep));
  dep[1] = 0;
  fa[1] = 1;
  col[1] = 1;
  dfs(1, 1);
  int tx = sx, ty = sy;
  while (dep[tx] > dep[ty]) tx = fa[tx], leh++;
  while (dep[ty] > dep[tx]) ty = fa[ty], leh++;
  while (tx ^ ty) tx = fa[tx], ty = fa[ty], leh += 2;
  if (leh % 2 == 0) {
    int wt = 0, bl = 0;
    for (int i = 1; i <= n; i++) {
      if (dep[i] & 1)
        wt++;
      else
        bl++;
    }
    if (abs(wt - bl) & 1) {
      puts("-1");
      return 0;
    }
    long long ans = 0;
    for (int i = 2; i <= n; i++) ans += abs(siz[i]);
    ans += abs(siz[1]) / 2;
    printf("%lld\n", ans);
    return 0;
  }
  for (int i = 1; i <= n; i++) sz[i][1] = col[i];
  sz[sx][0] = 1;
  sz[sy][0] = -1;
  clfs(1, 1);
  if (sz[1][0] || sz[1][1]) {
    puts("-1");
    return 0;
  }
  long long ans = 0;
  b[++tot] = 0;
  for (int i = 1; i <= n; i++) {
    if (!sz[i][0])
      ans += abs(sz[i][1]);
    else {
      if (sz[i][0] == 1)
        b[++tot] = -sz[i][1];
      else
        b[++tot] = sz[i][1];
    }
  }
  sort(b + 1, b + 1 + tot);
  long long xv = b[(tot + 1) / 2];
  for (int i = 1; i <= n; i++)
    if (sz[i][0]) ans += abs(xv * sz[i][0] + sz[i][1]);
  printf("%lld\n", ans + abs(xv));
}
