#include <bits/stdc++.h>
inline void check_min(int a, int &b) {
  if (a < b) b = a;
}
namespace BoDaJingShen {
int abs(int x) { return x < 0 ? -x : x; }
const int N = 100100, M = N * 2;
struct disjoint_set_union {
  int q[N];
  void init(int n) {
    for (int i = 1; i <= n; i++) q[i] = i;
  }
  int ask(int p) { return p == q[p] ? p : q[p] = ask(q[p]); }
  inline void link(int u, int v) { q[ask(u)] = ask(v); }
  inline bool uni(int u, int v) { return ask(u) == ask(v); }
} d;
int begin[N], next[M], to[M];
int dep[N], fa[N], s[N];
bool fob[N];
int n, m, e, ans, sx, sy;
void add(int x, int y, bool k = 1) {
  to[++e] = y;
  next[e] = begin[x];
  begin[x] = e;
  if (k) add(y, x, 0);
}
void predfs(int p, int h = 0) {
  for (int i = begin[p], q; i; i = next[i])
    if ((q = to[i]) != h) fa[q] = p, dep[q] = dep[p] + 1, predfs(q, p);
}
int dfs(int p, int h = 0) {
  if (fob[p]) return 0;
  int ret = s[p];
  for (int i = begin[p], q; i; i = next[i])
    if ((q = to[i]) != h) ret += dfs(q, p);
  ans += abs(ret);
  return ret;
}
void initialize() {
  scanf("%d%d", &n, &m);
  d.init(n);
  for (int i = 1, u, v; i <= m; i++) {
    scanf("%d%d", &u, &v);
    if (d.uni(u, v))
      sx = u, sy = v;
    else
      add(u, v), d.link(u, v);
  }
  if (!sx) sx = 1;
  predfs(sx);
  for (int i = 1; i <= n; i++) s[i] = (dep[i] & 1) ? 1 : -1;
}
int t0[N * 8], *c = t0 + N * 4;
int stk[N], f[N * 2], tot;
inline void push(int &p) { fob[p] = 1, stk[++tot] = p, p = fa[p]; }
void work0() {
  while (1) printf("?\n");
  if (dfs(1)) {
    printf("-1\n");
    return;
  }
  tot = 0;
  while (sx != sy) push(sy);
  push(sx);
  ans = 0;
  for (int i = 1; i <= tot; i++) {
    fob[stk[i]] = 0;
    ans -= abs(f[i + tot] = f[i] = dfs(stk[i]));
    fob[stk[i]] = 1;
  }
  for (int i = 1; i <= tot; i++) ans += abs(f[i] += f[i - 1]), c[f[i]]++;
  int ret = ans, cnt = 0;
  for (int i = 1; i <= n; i++) cnt += c[i];
  for (int i = tot + 1; i < 2 * tot; i++) {
    int x = -f[i];
    if (x > 0) {
      while (x--) {
        ret += (n - cnt) - cnt;
        cnt += c[0];
        c = c - 1;
      }
      c[0]--;
    } else {
      x = -x;
      while (x--) {
        ret += cnt + c[0] - (n - cnt - c[0]);
        c = c + 1;
        cnt -= c[0];
      }
      c[0]--;
    }
    ret += abs(f[i] += f[i - 1]);
    c[f[i]]++;
    if (f[i] > 0) cnt++;
    check_min(ret, ans);
  }
  printf("%d\n", ans);
}
void work1() {
  int tmp = 0, k;
  for (int i = 1; i <= n; i++) tmp += s[i];
  if (tmp & 1) {
    printf("-1\n");
    return;
  }
  ans = abs(tmp / 2);
  k = tmp > 0 ? -1 : 1;
  s[sx] += k * ans;
  s[sy] += k * ans;
  if (dfs(sx)) printf("fuck\n");
  printf("%d\n", ans);
}
void solve() {
  initialize();
  if (m == n - 1) {
    ans = 0;
    if (dfs(sx) == 0)
      printf("%d\n", ans);
    else
      printf("-1\n");
  } else {
    if ((dep[sx] & 1) == (dep[sy] & 1))
      work1();
    else
      work0();
  }
}
}  // namespace BoDaJingShen
int main() {
  BoDaJingShen::solve();
  return 0;
}
