#include <bits/stdc++.h>
using namespace std;
long long int ans = 0;
long long int lol, h[100050], n, m, x, y, S, T, k[100050], siz[100050],
    s[100050], top;
bool pd;
struct abc {
  int q, w;
} di[100005];
inline void more(int x, int y) {
  di[++lol].q = y;
  di[lol].w = h[x], h[x] = lol;
}
int in() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x;
}
void work1(int xx, int yy) {
  int i;
  for (i = h[xx]; i != 0; i = di[i].w) {
    if (di[i].q == yy) {
      continue;
    }
    if (siz[di[i].q]) {
      if (siz[xx] == siz[di[i].q]) {
        pd = 1;
        S = xx, T = di[i].q;
      }
    } else {
      siz[di[i].q] = -siz[xx];
      work1(di[i].q, xx);
    }
  }
}
void work2(int xx, int yy) {
  int i;
  for (i = h[xx]; i != 0; i = di[i].w) {
    if (di[i].q == yy || (xx == S && di[i].q == T) ||
        (xx == T && di[i].q != S)) {
      continue;
    }
    work2(di[i].q, xx);
    siz[xx] += siz[di[i].q];
    k[xx] += k[di[i].q];
  }
}
void main_work() {
  n = in();
  m = in();
  int i;
  for (i = 0; i < m; i++) {
    x = in();
    y = in();
    more(x, y);
    more(y, x);
  }
  siz[1] = 1;
  work1(1, 0);
  int sum = 0;
  for (i = 1; i <= n; i++) {
    sum += siz[i];
  }
  if (m == n - 1) {
    if (sum != 0) {
      puts("-1");
      exit(0);
    }
  } else {
    if (pd == 1) {
      if (sum & 1) {
        puts("-1");
        exit(0);
      }
      ans += abs(sum / 2);
      siz[S] -= sum / 2;
      siz[T] -= sum / 2;
    } else {
      if (sum) {
        puts("-1");
        exit(0);
      } else {
        k[S] = 1;
        k[T] = -1;
      }
    }
  }
  work2(1, 0);
  for (i = 1; i <= n; i++) {
    if (k[i]) {
      s[++top] = k[i] * siz[i];
    } else {
      ans += abs(siz[i]);
    }
  }
  top++;
  s[top] = 0;
  sort(s + 1, s + top + 1);
  int qq = s[(top + 1) >> 1];
  for (i = 1; i <= top; i++) {
    if (k[i]) {
      ans += abs(s[i] - qq);
    }
  }
  cout << ans;
  exit(0);
}
int main() {
  main_work();
  return 0;
}
