#include <bits/stdc++.h>
using namespace std;
int head[100050], to[100050 << 1], nxt[100050 << 1], cnt, n, m, dep[100050],
    vis[100050], siz[100050], p1, p2, w[100050];
int k[100050], a[100050], la;
inline void add(int u, int v) {
  to[++cnt] = v;
  nxt[cnt] = head[u];
  head[u] = cnt;
}
void dfs(int x, int y) {
  vis[x] = 1;
  dep[x] = dep[y] + 1;
  if (dep[x] & 1) {
    siz[x] = 1;
  } else {
    siz[x] = -1;
  }
  int i;
  for (i = head[x]; i; i = nxt[i]) {
    if (to[i] != y && !vis[to[i]]) {
      dfs(to[i], x);
      siz[x] += siz[to[i]];
    } else if (to[i] != y) {
      p1 = x, p2 = to[i];
    }
  }
}
void dfs2(int x, int y) {
  int i;
  vis[x] = 1;
  if (dep[x] & 1)
    siz[x]++;
  else
    siz[x]--;
  for (i = head[x]; i; i = nxt[i])
    if (to[i] != y && !vis[to[i]]) {
      dfs2(to[i], x);
      siz[x] += siz[to[i]];
      k[x] += k[to[i]];
    }
}
int main() {
  scanf("%d%d", &n, &m);
  int i, x, y;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  dfs(1, 0);
  if (m == n - 1) {
    if (siz[1]) {
      puts("-1");
      return 0;
    }
    long long ans = 0;
    for (i = 1; i <= n; i++) ans += abs(siz[i]);
    printf("%lld\n", ans);
  } else if ((dep[p2] - dep[p1] + 1) & 1) {
    if (abs(siz[1]) & 1) {
      puts("-1");
      return 0;
    }
    long long ans = abs(siz[1]) / 2;
    memset(vis, 0, sizeof(vis));
    memset(siz, 0, sizeof(siz));
    siz[p1] -= siz[1] / 2, siz[p2] -= siz[1] / 2;
    dfs2(1, 0);
    for (i = 1; i <= n; i++) ans += abs(siz[i]);
    printf("%lld\n", ans);
  } else {
    if (siz[1]) {
      puts("-1");
      return 0;
    }
    memset(vis, 0, sizeof(vis));
    memset(siz, 0, sizeof(siz));
    k[p1] = 1, k[p2] = -1;
    dfs2(1, 0);
    long long ans = 0;
    for (i = 1; i <= n; i++) {
      if (!k[i])
        ans += abs(siz[i]);
      else if (k[i] == 1)
        a[++la] = -siz[i];
      else
        a[++la] = siz[i];
    }
    sort(a + 1, a + la + 1);
    int v = a[(la + 1) >> 1];
    for (i = 1; i <= n; i++) ans += abs(v - a[i]);
    printf("%lld\n", ans);
  }
}
