#include <bits/stdc++.h>
inline int read() {
  int data = 0, w = 1;
  char ch = getchar();
  while (ch != '-' && (!isdigit(ch))) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
  return data * w;
}
const int maxn(1e5 + 10);
struct edge {
  int next, to;
} e[maxn << 1];
int head[maxn], e_num;
inline void add_edge(int from, int to) {
  e[++e_num] = (edge){head[from], to};
  head[from] = e_num;
}
int n, m, dep[maxn], s[maxn], k[maxn], len, X, Y, stk[maxn], top, sum;
long long ans;
void dfs(int x, int fa) {
  dep[x] = dep[fa] + 1;
  s[x] = (dep[x] & 1) ? 1 : -1;
  sum += s[x];
  for (register int i = head[x]; i; i = e[i].next) {
    int to = e[i].to;
    if (to == fa) continue;
    if (dep[to])
      len = abs(dep[x] - dep[to]) + 1, X = x, Y = to;
    else
      dfs(to, x);
  }
}
void dfs2(int x, int fa) {
  for (register int i = head[x]; i; i = e[i].next) {
    int to = e[i].to;
    if (to == fa) continue;
    if ((X == x && Y == to) || (X == to && Y == x)) continue;
    dfs2(to, x), k[x] += k[to], s[x] += s[to];
  }
  if (!k[x])
    ans += abs(s[x]);
  else
    stk[++top] = k[x] * s[x];
}
int main() {
  n = read(), m = read();
  for (register int i = 1, a, b; i <= m; i++)
    a = read(), b = read(), add_edge(a, b), add_edge(b, a);
  dfs(1, 0);
  if (m == n - 1) {
    if (sum) return puts("-1"), 0;
  } else if (len & 1) {
    if (sum % 2 == 1) return puts("-1"), 0;
    ans += abs(sum / 2), s[X] -= sum / 2, s[Y] -= sum / 2;
  } else {
    if (sum) return puts("-1"), 0;
    k[X] = 1, k[Y] = -1;
  }
  dfs2(1, 0);
  stk[++top] = 0;
  std::sort(stk + 1, stk + top + 1);
  for (register int i = 1; i <= n; i++) ans += abs(stk[(top + 1) / 2] - stk[i]);
  std::cout << ans << std::endl;
  return 0;
}
