#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int h[maxn], go[maxn * 2], nxt[maxn * 2], fa[maxn], co[maxn], kk[maxn],
    bb[maxn], sta[maxn];
int i, j, k, l, t, n, m, u, v, a, b, x, tot, top, num;
long long ans;
bool czy, gjx;
void add(int x, int y) {
  go[++tot] = y;
  nxt[tot] = h[x];
  h[x] = tot;
}
int getfa(int x) {
  if (!fa[x]) return x;
  int t = getfa(fa[x]);
  co[x] = co[x] ^ co[fa[x]];
  return fa[x] = t;
}
void dfs(int x, int y) {
  int t = h[x];
  while (t) {
    if (go[t] != y) {
      dfs(go[t], x);
      kk[x] += kk[go[t]];
      bb[x] += bb[go[t]];
    }
    t = nxt[t];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &j, &k);
    a = getfa(j);
    b = getfa(k);
    if (a != b) {
      add(j, k);
      add(k, j);
      fa[a] = b;
      co[a] = co[j] ^ co[k] ^ 1;
    } else {
      u = j;
      v = k;
      if (co[j] == co[k]) gjx = 1;
    }
  }
  for (i = 1; i <= n; i++) getfa(i);
  if (m == n - 1) {
    for (i = 1; i <= n; i++)
      if (co[i])
        num++;
      else
        num--;
    if (num) {
      printf("-1\n");
      return 0;
    }
    for (i = 1; i <= n; i++) {
      kk[i] = 0;
      if (co[i])
        bb[i] = 1;
      else
        bb[i] = -1;
    }
  } else {
    if (gjx) {
      if (co[u])
        for (i = 1; i <= n; i++) co[i] ^= 1;
      for (i = 1; i <= n; i++)
        if (co[i])
          num++;
        else
          num--;
      if (num % 2 == 1) {
        printf("-1\n");
        return 0;
      }
      for (i = 1; i <= n; i++) {
        kk[i] = 0;
        if (co[i])
          bb[i] = 1;
        else
          bb[i] = -1;
      }
      bb[u] -= num / 2;
      bb[v] -= num / 2;
      ans += num / 2;
    } else {
      for (i = 1; i <= n; i++)
        if (co[i])
          num++;
        else
          num--;
      if (num) {
        printf("-1\n");
        return 0;
      }
      for (i = 1; i <= n; i++) {
        kk[i] = 0;
        if (co[i])
          bb[i] = 1;
        else
          bb[i] = -1;
      }
      kk[u] = 1;
      kk[v] = -1;
    }
  }
  dfs(1, 0);
  for (i = 1; i <= n; i++)
    if (!kk[i])
      ans += (long long)abs(bb[i]);
    else {
      if (kk[i]) bb[i] = -bb[i];
      sta[++top] = bb[i];
    }
  sta[++top] = 0;
  sort(sta + 1, sta + top + 1);
  x = sta[(top + 1) / 2];
  for (i = 1; i <= n; i++) ans += (long long)abs(sta[i] - x);
  printf("%lld\n", ans);
}
