#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n, m, i, j, u, v, from, trans;
int odd, even, ans, delta;
int pn, p[N], s[N], bw[2], cnt[2];
int En, fst[N], vis[N], col[N], nxt[N * 2], to[N * 2], have[N];
void add(int u, int v) {
  En++;
  nxt[En] = fst[u];
  fst[u] = En;
  to[En] = v;
}
void dfs(int x, int fa, int flg) {
  int j, v;
  vis[x] = 1;
  col[x] = flg;
  cnt[flg]++;
  for (j = fst[x]; j; j = nxt[j]) {
    v = to[j];
    if (v == fa) continue;
    if (vis[v]) {
      trans = v;
      from = x;
      if (col[x] == col[v])
        odd = 1;
      else
        even = 1;
      continue;
    }
    dfs(to[j], x, flg ^ 1);
  }
}
void dfs2(int x, int fa) {
  int j, v;
  if (trans == x && even) have[x] = 1;
  for (j = fst[x]; j; j = nxt[j]) {
    v = to[j];
    if (x == from && v == trans) continue;
    if (x == trans && v == from) continue;
    if (v == fa) continue;
    dfs2(v, x);
    if (have[v]) have[x] = 1;
    s[x] += s[v];
  }
  if (x != from) {
    if (have[x])
      p[++pn] = s[x];
    else
      ans += abs(s[x]);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  dfs(1, 1, 0);
  bw[0] = 0;
  bw[1] = 1;
  if (cnt[0] < cnt[1]) swap(bw[0], bw[1]);
  for (i = 1; i <= n; i++) {
    col[i] = bw[col[i]];
    if (col[i])
      s[i] = 1;
    else
      s[i] = -1;
  }
  if (odd) {
    delta = abs(cnt[0] - cnt[1]);
    if (delta % 2) {
      printf("-1\n");
      return 0;
    }
    ans += delta / 2;
    s[trans] += delta / 2;
  } else if (cnt[0] != cnt[1]) {
    printf("-1\n");
    return 0;
  }
  if (!from) from = 1;
  dfs2(from, from);
  if (even) {
    p[++pn] = 0;
    sort(p + 1, p + 1 + pn);
    for (i = 1; i <= pn; i++) ans += abs(p[i] - p[pn / 2 + 1]);
  }
  printf("%d\n", ans);
  return 0;
}
