#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') t = true, ch = getchar();
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
struct Line {
  int v, next;
} e[100100 << 1];
int h[100100], cnt = 1;
inline void Add(int u, int v) {
  e[cnt] = (Line){v, h[u]};
  h[u] = cnt++;
}
int n, m, dep[100100], s[100100], k[100100], ln, U, V, S[100100], top;
long long ans;
void dfs(int u, int ff) {
  dep[u] = dep[ff] + 1;
  s[u] = (dep[u] & 1) ? 1 : -1;
  for (int i = h[u]; i; i = e[i].next) {
    int v = e[i].v;
    if (v == ff) continue;
    if (dep[v] && !U)
      ln = abs(dep[u] - dep[v]) + 1, U = u, V = v;
    else
      dfs(v, u), s[u] += s[v];
  }
  ans += abs(s[u]);
}
void dfs2(int u, int ff) {
  for (int i = h[u]; i; i = e[i].next) {
    int v = e[i].v;
    if (v == ff || (U == u && V == v) || (U == v && V == u)) continue;
    dfs2(v, u), k[u] += k[v];
  }
  if (k[u] == 0)
    ans += abs(s[u]);
  else
    S[++top] = k[u] * s[u];
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; ++i) {
    int a = read(), b = read();
    Add(a, b);
    Add(b, a);
  }
  dfs(1, 0);
  if (m == n - 1) {
    if (s[1] != 0)
      puts("-1");
    else
      cout << ans << endl;
  } else if (ln & 1) {
    return 1;
    if (s[1] % 2 == 1)
      puts("-1");
    else
      cout << ans + abs(s[1]) / 2 << endl;
  } else {
    if (s[1] != 0) {
      puts("-1");
      return 0;
    }
    k[U] = 1;
    k[V] = -1;
    ans = 0;
    dfs2(1, 0);
    S[++top] = 0;
    sort(&S[1], &S[top + 1]);
    for (int i = 1; i <= top; ++i) ans += abs(S[(top + 1) / 2] - S[i]);
    cout << ans << endl;
  }
  return 0;
}
