#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct E {
  int v, nt;
} e[N << 1];
int n, m, su, sv, cri, tot, h[N], s[N];
int f[N], g[N], A[N], dep[N], vis[N];
long long ans;
inline int abs(int x) { return x < 0 ? -x : x; }
inline void add(int u, int v) {
  e[++tot] = (E){v, h[u]}, h[u] = tot;
  e[++tot] = (E){u, h[v]}, h[v] = tot;
}
template <class T>
inline void in(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9';) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  for (; ch >= '0' && ch <= '9';) x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
inline void dfs(int x) {
  vis[x] = 1;
  if (dep[x] & 1)
    f[x] = 1;
  else
    f[x] = -1;
  for (int v, i = h[x]; i; i = e[i].nt)
    if (!vis[v = e[i].v])
      dep[v] = dep[x] + 1, dfs(v);
    else if (dep[v] < dep[x])
      cri = dep[x] - dep[v] + 1, su = v, sv = x;
}
inline void dfs(int x, int p) {
  s[x] = f[x];
  for (int v, i = h[x]; i; i = e[i].nt)
    if (((v = e[i].v) == p) || (x == su && v == sv) || (v == su && x == sv))
      continue;
    else
      dfs(v, x), s[x] += s[v], g[x] += g[v];
}
int main() {
  int u, v;
  in(n), in(m);
  for (int i = 1; i <= m; ++i) in(u), in(v), add(u, v);
  dfs(1);
  if (m == n - 1) {
    dfs(1, 0);
    if (s[1]) {
      puts("-1");
      return 0;
    }
    for (int i = 1; i <= n; ++i) ans += abs(s[i]);
  } else if (cri & 1) {
    dfs(1, 0);
    if (s[1] % 2) {
      puts("-1");
      return 0;
    }
    int tmp = s[1];
    f[su] -= tmp >> 1, f[sv] -= tmp >> 1, ans = abs(tmp >> 1);
    dfs(1, 0);
    for (int i = 1; i <= n; ++i) ans += (long long)abs(s[i]);
  } else {
    ++g[su], --g[sv];
    dfs(1, 0);
    if (s[1]) {
      puts("-1");
      return 0;
    }
    int tmp, num = 0;
    for (int i = 1; i <= n; ++i)
      if (!g[i])
        ans += (long long)abs(s[i]);
      else if (g[i])
        A[++num] = s[i];
      else
        A[++num] = -s[i];
    A[++num] = 0;
    sort(A + 1, A + num + 1);
    tmp = A[num + 1 >> 1];
    for (int i = 1; i <= num; ++i) ans += (long long)abs(tmp - A[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
