#include <bits/stdc++.h>
const int MaxN = 100010;
namespace Steaunk {
int n, m, ans, s[MaxN], sta[MaxN], tot, a[MaxN];
int Point[MaxN], Next[MaxN << 1], To[MaxN << 1], q = 1;
bool vis[MaxN];
void Add(int u, int v) {
  Next[++q] = Point[u];
  Point[u] = q;
  To[q] = v;
  Next[++q] = Point[v];
  Point[v] = q;
  To[q] = u;
}
void dfs(int u, int from) {
  s[u] = 0;
  for (int j = Point[u]; j; j = Next[j])
    if (To[j] != from && !vis[To[j]]) dfs(To[j], u);
  if (u != from) {
    s[from] += 1 - s[u];
    ans += (1 - s[u] > 0 ? 1 - s[u] : -(1 - s[u]));
    s[u] = 1;
  }
}
int dfs1(int u, int from) {
  vis[u] = 1;
  for (int j = Point[u]; j; j = Next[j])
    if (j != (from ^ 1)) {
      if (vis[To[j]]) {
        vis[sta[tot++] = u] = 0;
        return u;
      }
      int t;
      if (!vis[To[j]] && (t = dfs1(To[j], j))) {
        vis[u] = 0;
        if (~t)
          return (sta[tot++] = u) == t ? -1 : t;
        else
          return t;
      }
    }
  vis[u] = 0;
  return 0;
}
void main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) Point[i] = 0;
  q = 1;
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    Add(u, v);
  }
  ans = 0;
  if (m == n - 1) {
    dfs(1, 1);
    if (s[1] != 1)
      puts("-1");
    else
      printf("%d\n", ans);
  } else {
    tot = 0;
    dfs1(1, 0);
    for (int *i = sta; i < sta + tot; ++i) vis[*i] = 1;
    for (int *i = sta; i < sta + tot; ++i) dfs(*i, *i);
    for (int *i = sta; i < sta + tot; ++i) vis[*i] = 0;
    sta[tot] = sta[0];
    for (int i = 1; i < tot; ++i) a[i] = 1 - s[sta[i - 1]] - a[i - 1];
    a[tot] = 1 - s[sta[tot - 1]];
    if (tot & 1) {
      if (a[tot] + a[tot - 1] & 1)
        puts("-1");
      else {
        int t = (a[tot] - a[tot - 1]) / 2;
        for (int i = 0; i < tot; ++i)
          if (i & 1)
            ans += (a[i] - t > 0 ? a[i] - t : -(a[i] - t));
          else
            ans += (a[i] + t > 0 ? a[i] + t : -(a[i] + t));
        printf("%d\n", ans);
      }
    } else {
      if (a[tot] != a[tot - 1])
        puts("-1");
      else {
        for (int i = 0; i < tot; ++i)
          if (i & 1)
            s[i] = a[i];
          else
            s[i] = -a[i];
        std::nth_element(s, s + (tot >> 1), s + tot);
        int t = s[tot >> 1];
        for (int i = 0; i < tot; ++i)
          if (i & 1)
            ans += (a[i] - t > 0 ? a[i] - t : -(a[i] - t));
          else
            ans += (a[i] + t > 0 ? a[i] + t : -(a[i] + t));
        printf("%d\n", ans);
      }
    }
  }
}
}  // namespace Steaunk
int main() {
  int T;
  Steaunk::main();
  return 0;
}
