#include <bits/stdc++.h>
using namespace std;
int n, m, tx, ty, tot, tp, a[100009], q[100009], fst[100009], pnt[100009 << 1],
    nxt[100009 << 1];
int f[100009], g[100009], c[100009], d[100009], fa[100009];
bool vis[100009], dlt[100009];
long long ans;
void add(int x, int y) {
  pnt[++tot] = y;
  nxt[tot] = fst[x];
  fst[x] = tot;
}
void dfs(int x) {
  int i, y;
  vis[x] = 1;
  f[x] = d[x] & 1;
  g[x] = f[x] ^ 1;
  for (i = fst[x]; i; i = nxt[i]) {
    y = pnt[i];
    if (dlt[y]) continue;
    if (!vis[y]) {
      fa[y] = x;
      d[y] = d[x] ^ 1;
      dfs(y);
      f[x] += f[y];
      g[x] += g[y];
    } else if (y != fa[x] && !tx) {
      tx = x;
      ty = y;
    }
  }
  c[x] = f[x] - g[x];
  if (fa[x] && !dlt[x]) ans += abs(c[x]);
}
void gg() {
  puts("-1");
  exit(0);
}
int main() {
  scanf("%d%d", &n, &m);
  if (n & 1) gg();
  int i, x, y;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  dfs(1);
  if (!tx) tx = ty = 1;
  if (!(d[tx] ^ d[ty])) {
    if (c[1] & 1) gg();
    x = c[1] / 2;
  }
  for (i = tx; i != ty; i = fa[i]) q[++tp] = i;
  q[++tp] = ty;
  for (i = 1; i <= tp; i++) dlt[q[i]] = 1;
  ans = 0;
  memset(vis, 0, sizeof(vis));
  for (i = 1; i <= tp; i++) dfs(q[i]);
  for (i = 1; i < tp; i++) a[i] = a[i - 1] + c[q[i]];
  if (d[tx] ^ d[ty]) {
    sort(a + 1, a + tp + 1);
    x = a[tp + 1 >> 1];
  }
  for (i = 1; i <= tp; i++) ans += abs(x - a[i]);
  printf("%lld\n", ans);
  return 0;
}
