#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
struct node {
  int x, y, next;
} a[100010 * 2];
int len, first[100010];
int mymin(int x, int y) { return (x < y) ? x : y; }
void ins(int x, int y) {
  len++;
  a[len].x = x;
  a[len].y = y;
  a[len].next = first[x];
  first[x] = len;
}
int ans = 0, ff[100010];
int ffind(int x) { return (x == ff[x]) ? x : ff[x] = ffind(ff[x]); }
int whi[100010], col[100010], siz[100010], bak[100010], w[100010], dep[100010],
    fa[100010];
void treedp(int x) {
  bak[x] = col[x];
  siz[x] = 1;
  for (int k = first[x]; k != -1; k = a[k].next) {
    int y = a[k].y;
    if (y == fa[x]) continue;
    col[y] = 1 - col[x];
    dep[y] = dep[x] + 1;
    fa[y] = x;
    treedp(y);
    bak[x] += bak[y];
    siz[x] += siz[y];
  }
  whi[x] = siz[x] - bak[x];
}
int A[100010], cnt, B[100010], bl, C[100010], cl;
void mov(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  while (dep[x] > dep[y]) {
    A[++cnt] = -(whi[x] - bak[x]);
    B[++bl] = x;
    x = fa[x];
  }
  while (x != y) {
    A[++cnt] = -(whi[x] - bak[x]);
    A[++cnt] = (whi[x] - bak[y]);
    B[++bl] = x;
    C[++cl] = y;
    x = fa[x];
    y = fa[y];
  }
}
int get_ans(int x) {
  int ret = 0;
  bak[x] = col[x];
  for (int k = first[x]; k != -1; k = a[k].next) {
    int y = a[k].y;
    if (y == fa[x]) continue;
    ret += get_ans(y);
    bak[x] += bak[y];
    ret += abs(whi[y] - bak[y] + w[y]);
  }
  return ret;
}
int main() {
  int T, n, m, x, y, i, s, t;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) ff[i] = i;
  len = 0;
  memset(first, -1, sizeof(first));
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    int fx = ffind(x), fy = ffind(y);
    if (fx != fy) {
      ins(x, y);
      ins(y, x);
      ff[fx] = fy;
    } else {
      s = x;
      t = y;
    }
  }
  dep[1] = 1;
  fa[1] = 0;
  col[1] = 1;
  treedp(1);
  memset(w, 0, sizeof(w));
  if (m == n - 1) {
    if (bak[1] * 2 != siz[1])
      printf("-1\n");
    else
      printf("%d\n", get_ans(1));
  } else {
    if (col[s] == col[t]) {
      if ((siz[1] - bak[1]) & 1)
        printf("-1\n");
      else {
        int ls = (whi[1] - bak[1]) / 2;
        col[s] += ls;
        col[t] += ls;
        printf("%d\n", get_ans(1) + ls);
      }
    } else {
      bl = cl = cnt = 0;
      mov(s, t);
      sort(A + 1, A + 1 + cnt);
      int mid = A[(cnt + 1) / 2];
      for (i = 1; i <= bl; i++) w[B[i]] += mid;
      for (i = 1; i <= cl; i++) w[C[i]] -= mid;
      printf("%d\n", get_ans(1) + abs(mid));
    }
  }
  return 0;
}
