#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read() {
  T f = 0, x = 0;
  char c = getchar();
  while (!isdigit(c)) f = c == '-', c = getchar();
  while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
namespace run {
const int N = 1e5 + 9;
int head[N], nex[N << 1], to[N << 1], cnt;
inline void add(int u, int v) {
  nex[++cnt] = head[u];
  head[u] = cnt, to[cnt] = v;
}
int dep[N], siz[N], sum[N], fa[N];
inline void dfs(int u, int _f) {
  siz[u] = 1, dep[u] = dep[_f] + 1, sum[u] = (dep[u] & 1) * 2 - 1;
  for (int i = head[u]; i; i = nex[i])
    if (to[i] ^ _f) dfs(to[i], u), siz[u] += siz[to[i]], sum[u] += sum[to[i]];
}
int vis[N], S, T;
inline void dfs1(int u, int _f) {
  vis[u] = 1, siz[u] = 1, dep[u] = dep[_f] + 1, sum[u] = (dep[u] & 1) * 2 - 1;
  for (int i = head[u]; i; i = nex[i])
    if (to[i] ^ _f) {
      if (!vis[to[i]])
        dfs1(to[i], u), siz[u] += siz[to[i]], sum[u] += sum[to[i]];
      else
        S = u, T = to[i];
    }
}
int k[N];
inline void dfs2(int u, int _f) {
  vis[u] = 1;
  for (int i = head[u]; i; i = nex[i])
    if (to[i] ^ _f)
      if (!vis[to[i]]) dfs2(to[i], u), k[u] += k[to[i]];
}
int n, m, st[N], top, ans;
int main() {
  n = read<int>(), m = read<int>();
  for (int i = 1; i <= m; i++) {
    int u = read<int>(), v = read<int>();
    add(u, v), add(v, u);
  }
  if (m == n - 1) {
    dfs(1, 0);
    if (sum[1]) puts("-1"), exit(0);
    int ans = 0;
    for (int i = 1; i <= n; i++) ans += abs(sum[i]);
    printf("%d\n", ans);
  } else {
    dfs1(1, 0);
    if ((dep[S] & 1) == (dep[T] & 1)) {
      if (sum[1] & 1) puts("-1"), exit(0);
      int del = sum[1] / 2;
      ans = abs(del);
      for (int i = S; i; i = fa[i]) sum[i] -= del;
      for (int i = T; i; i = fa[i]) sum[i] -= del;
      for (int i = 1; i <= n; i++) ans += abs(sum[i]);
    } else {
      for (int i = 1; i <= n; i++) vis[i] = 0;
      k[S] = 1, k[T] = -1, dfs2(1, 0);
      for (int i = 1; i <= n; i++)
        if (k[i])
          st[++top] = k[i] * sum[i];
        else
          ans += abs(sum[i]);
      nth_element(st + 1, st + top + 1, st + ((top + 1) >> 1));
      int pos = (top + 1) >> 1;
      for (int i = 1; i <= top; i++) ans += abs(st[i] - st[pos]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
}  // namespace run
int main() { return run::main(); }
