#include <bits/stdc++.h>
const int N = 100054, M = N * 2;
int n, m, E = 0, L;
int to[M], first[N], next[M];
int p[N], dep[N], ssize[N];
int cnt = 0, buf[N];
inline void addedge(int u, int v) {
  to[++E] = v, next[E] = first[u], first[u] = E;
  to[++E] = u, next[E] = first[v], first[v] = E;
}
void dfs(int x) {
  int i, y;
  ssize[x] = -(dep[x] & 1) | 1;
  for (i = first[x]; i; i = next[i])
    if (!~p[y = to[i]])
      p[y] = x, dep[y] = dep[x] + 1, dfs(y), ssize[x] += ssize[y];
    else if (dep[x] < dep[y])
      L = i;
  fprintf(stderr, "ssize[%d] = %d\n", x, ssize[x]);
}
int main() {
  int i, u, v, incr;
  long long ans = 0;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; ++i) scanf("%d%d", &u, &v), addedge(u, v);
  memset(p, -1, sizeof p), p[1] = 0, dfs(1);
  if (m == n - 1) {
    if (ssize[1]) return puts("-1"), 0;
    for (i = 1; i <= n; ++i) ans += abs(ssize[i]);
    return printf("%lld\n", ans), 0;
  }
  u = to[((L - 1 ^ 1) + 1)], v = to[L];
  assert(m == n && dep[u] < dep[v]);
  if ((dep[u] ^ dep[v]) & 1) {
    if (ssize[1]) return puts("-1"), 0;
    for (i = v; i != u; i = p[i]) buf[cnt++] = ssize[i];
    buf[cnt++] = ssize[i];
    std::nth_element(buf, buf + n / 2, buf + n);
    for (i = 0; i < cnt; ++i) ans += abs(buf[i] - buf[n / 2]) - abs(buf[i]);
  } else {
    if (ssize[1] & 1) return puts("-1"), 0;
    incr = -ssize[1] / 2, ans = abs(incr);
    for (i = v; i != u; i = p[i]) ssize[i] += incr;
    for (; i; i = p[i]) ssize[i] += 2 * incr;
  }
  for (i = 1; i <= n; ++i) ans += abs(ssize[i]);
  printf("%lld\n", ans);
  return 0;
}
