#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') t = -1;
    c = getchar();
  }
  while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
  return s * t;
}
const int N = 1e5 + 5;
struct edge {
  int to, nxt;
} e[N << 1];
int head[N], cnt = 0;
inline void add(int x, int y) {
  e[++cnt].to = y, e[cnt].nxt = head[x], head[x] = cnt;
  e[++cnt].to = x, e[cnt].nxt = head[y], head[y] = cnt;
}
long long ans = 0;
int u, v, f[N], g[N], A[N], tot = 0, col[N];
bool flag;
inline void dfs(int x, int fa = 0) {
  f[x] = -f[fa];
  for (register int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != fa) {
      if (col[e[i].to]) {
        u = x, v = e[i].to;
        if (col[e[i].to] == col[x])
          flag = 0;
        else
          flag = 1;
      } else {
        col[e[i].to] = -col[x];
        dfs(e[i].to, x);
      }
    }
}
inline void dp(int x, int fa = 0) {
  for (register int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != fa) {
      if (x == u && v == e[i].to) continue;
      if (x == v && u == e[i].to) continue;
      dp(e[i].to, x), f[x] += f[e[i].to], g[x] += g[e[i].to];
    }
}
inline void out() { puts("-1"), exit(0); }
int main() {
  int n = read(), m = read();
  for (register int i = 1; i <= m; ++i) add(read(), read());
  f[0] = 1, col[1] = 1, dfs(1);
  if (m == n - 1) {
    dp(1);
    if (f[1]) out();
    for (register int i = 1; i <= n; ++i) ans += abs(f[i]);
  } else if (!flag) {
    dp(1);
    if (f[1] % 2) out();
    memset(col, 0, sizeof(col)), f[0] = 1, col[1] = 1, dfs(1);
    f[u] -= (f[1] >> 1), f[v] -= (f[1] >> 1), ans = abs(f[1]) >> 1;
    dp(1);
    for (register int i = 1; i <= n; ++i) ans += abs(f[i]);
  } else {
    g[u]--, g[v]++;
    dp(1);
    if (f[1]) out();
    for (register int i = 1; i <= n; ++i)
      if (!g[i])
        ans += abs(f[i]);
      else if (g[i] == 1)
        A[++tot] = -f[i];
      else
        A[++tot] = f[i];
    A[++tot] = 0;
    sort(A + 1, A + tot + 1);
    for (register int i = 1; i <= tot; ++i)
      ans += abs(A[i] - A[(tot + 1) >> 1]);
  }
  cout << ans << endl;
  return 0;
}
