#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e17;
struct edge {
  int k, next;
} e[100005 << 1];
int home[100005], cnt = -1, n, m, fa[100005], sx, sy, dep[100005];
long long f[100005], w[100005];
int find(int k) {
  if (fa[k] != k) fa[k] = find(fa[k]);
  return fa[k];
}
void add(int x, int y) {
  cnt++;
  e[cnt].k = y;
  e[cnt].next = home[x];
  home[x] = cnt;
}
long long Abs(long long a) { return a < 0 ? -a : a; }
int op = 0;
void dfs(int k, int F, int dep) {
  if (dep)
    w[k] = 1;
  else
    w[k] = -1;
  ::dep[k] = dep;
  f[k] = 0;
  for (int i = home[k]; ~i; i = e[i].next) {
    if (e[i].k != F) {
      dfs(e[i].k, k, dep ^ 1);
      f[k] += f[e[i].k] + Abs(w[e[i].k]);
      w[k] += w[e[i].k];
    }
  }
}
void dfs1(int k, int F, int W) {
  dep[k] = dep[F] ^ 1;
  f[k] = w[k] = 0;
  if (dep[k])
    w[k] = 1;
  else
    w[k] = -1;
  for (int i = home[k]; ~i; i = e[i].next) {
    if (e[i].k != F) {
      dfs1(e[i].k, k, W);
      f[k] += f[e[i].k] + Abs(w[e[i].k]);
      w[k] += w[e[i].k];
    }
  }
  if (op == 1) {
    if (k == sy) w[k] -= W;
    if (k == sx) f[k] += Abs(W), w[k] += W;
  } else {
    if (k == sy) w[k] += W;
    if (k == sx) w[k] += W;
  }
}
int main() {
  memset(home, -1, sizeof(home));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    if (find(x) != find(y)) {
      add(x, y);
      add(y, x);
      fa[find(x)] = find(y);
    } else {
      sx = x;
      sy = y;
    }
  }
  dfs(1, 0, 1);
  long long ans = f[1];
  if (w[1]) ans = inf;
  if (n == m) {
    if (dep[sx] != dep[sy]) {
      if (w[1]) return puts("-1"), 0;
      op = 1;
      int l = -n, r = n, L, R;
      while (l <= r) {
        L = l + (r - l) / 3;
        R = r - (r - l) / 3;
        dfs1(sx, 0, L);
        long long wl = w[sx] == 0 ? f[sx] : inf;
        dfs1(sx, 0, R);
        long long wr = w[sx] == 0 ? f[sx] : inf;
        if (wl < wr)
          ans = min(ans, wl), r = R - 1;
        else
          l = L + 1, ans = min(ans, wr);
      }
      printf("%lld", ans);
      return 0;
    } else {
      if (w[1] % 2) return puts("-1"), 0;
      long long G = -w[1] / 2;
      op = 2;
      dfs1(sx, 0, G);
      ans = min(ans, f[sx] + Abs(G));
      printf("%lld", ans);
    }
    return 0;
  }
  if (ans == inf)
    puts("-1");
  else
    printf("%lld", ans);
  return 0;
}
