#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, next;
  edge(int _to = 0, int _next = 0) : to(_to), next(_next) {}
} e[100010 << 1];
int n, m, r1, r2;
int g[100010], nume, pre[100010], tag[100010];
int fa[100010];
int d[100010], delta;
int f1[100010], s1[100010];
int f2[100010], s2[100010];
void addEdge(int u, int v) {
  e[nume] = edge(v, g[u]);
  g[u] = nume++;
}
int getfa(int x) {
  if (fa[x] == x) return x;
  return fa[x] = getfa(fa[x]);
}
void dfs0(int x, int p) {
  pre[x] = p;
  for (int i = g[x]; ~i; i = e[i].next)
    if (e[i].to ^ p) {
      d[e[i].to] = !d[x];
      dfs0(e[i].to, x);
    }
}
void dfs1(int x, int p) {
  if (d[x])
    s1[x] = 1;
  else
    s1[x] = -1;
  for (int i = g[x]; ~i; i = e[i].next)
    if (e[i].to ^ p) {
      dfs1(e[i].to, x);
      s1[x] += s1[e[i].to];
      f1[x] += f1[e[i].to] + abs(s1[e[i].to]);
    }
}
void dfs2(int x, int p) {
  if (d[x])
    s2[x] = 1;
  else
    s2[x] = -1;
  for (int i = g[x]; ~i; i = e[i].next)
    if (e[i].to != p && !tag[e[i].to]) {
      dfs2(e[i].to, x);
      s2[x] += s2[e[i].to];
      f2[x] += f2[e[i].to] + abs(s2[e[i].to]);
    }
}
int main() {
  memset(g, -1, sizeof g);
  scanf("%d%d", &n, &m);
  if (n & 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (getfa(u) != getfa(v)) {
      fa[fa[u]] = fa[v];
      addEdge(u, v);
      addEdge(v, u);
    } else {
      r1 = u;
      r2 = v;
    }
  }
  if (!r1) r1 = 1;
  dfs0(r1, 0);
  if (r2) {
    for (int i = r2; i; i = pre[i]) tag[i] = 1;
  }
  int c1 = 0;
  for (int i = 1; i <= n; i++)
    if (d[i]) c1++;
  if ((n - 2 * c1) % 2 != 0) {
    puts("-1");
    return 0;
  }
  delta = (n - 2 * c1) / 2;
  if (!r2) {
    if (delta != 0) {
      puts("-1");
      return 0;
    }
    dfs1(r1, 0);
    printf("%d\n", f1[r1]);
  } else if (d[r1] != d[r2]) {
    if (delta != 0) {
      puts("-1");
      return 0;
    }
    static int b[100010];
    int numb = 0;
    int ans = 0, temp = 0;
    for (int i = r2; i; i = pre[i]) {
      dfs2(i, 0);
      ans += f2[i];
      temp += s2[i];
      b[++numb] = -temp;
    }
    sort(b + 1, b + numb + 1);
    int d0 = b[(numb + 1) / 2];
    for (int i = 1; i <= numb; i++) ans += abs(d0 - b[i]);
    printf("%d\n", ans);
  } else {
    assert(0);
  }
  return 0;
}
