#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, cnt = 1, G[N], c[N], ic[N], dfn[N], dpt[N];
struct edge {
  int t, nx;
} E[N << 2];
inline void addedge(int x, int y) {
  E[++cnt].t = y;
  E[cnt].nx = G[x];
  G[x] = cnt;
  E[++cnt].t = x;
  E[cnt].nx = G[y];
  G[y] = cnt;
}
int prty, u, v;
void dfs(int x, int cc) {
  c[x] = cc;
  for (int i = G[x]; i; i = E[i].nx) {
    if (!c[E[i].t]) dfs(E[i].t, cc ^ 1);
    if (c[E[i].t] == c[x]) prty = 1, u = x, v = E[i].t;
  }
}
int ans, a[N];
int dfs1(int x, int f) {
  int ret = a[x];
  for (int i = G[x]; i; i = E[i].nx)
    if (E[i].t != f && c[E[i].t] != c[x]) ret += dfs1(E[i].t, x);
  ans += abs(ret);
  return ret;
}
int k[N], Q[N], t, ct;
int dfs2(int x, int f) {
  int ret = a[x];
  for (int i = G[x]; i; i = E[i].nx)
    if (E[i].t != f && i != ct && (i ^ 1) != ct) {
      ret += dfs2(E[i].t, x);
      k[x] += k[E[i].t];
    }
  if (k[x]) {
    if (k[x] < 0)
      Q[++t] = -ret;
    else
      Q[++t] = ret;
  } else
    ans += abs(ret);
  return ret;
}
int vis[N];
void dfs3(int x, int f) {
  vis[x] = 1;
  for (int i = G[x]; i; i = E[i].nx) {
    if (E[i].t == f) continue;
    if (!vis[E[i].t])
      dfs3(E[i].t, x);
    else
      u = x, v = E[i].t, ct = i;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, x, y; i <= m; i++) scanf("%d%d", &x, &y), addedge(x, y);
  dfs(1, 0);
  int num = 0;
  for (int i = 1; i <= n; i++)
    if (c[i])
      num++, a[i] = 1;
    else
      num--, a[i] = -1;
  if (n - 1 == m) {
    if (num)
      puts("-1");
    else {
      dfs1(1, 0);
      printf("%d\n", ans);
    }
  } else {
    if (prty) {
      if (num & 1) puts("-1");
    } else {
      dfs3(1, 0);
      k[u] = 1;
      k[v] = -1;
      dfs2(1, 0);
      Q[++t] = 0;
      sort(Q + 1, Q + 1 + t);
      int cur = Q[t / 2];
      for (int i = 1; i <= t; i++) ans += abs(cur - Q[i]);
      printf("%d\n", ans);
    }
  }
  return 0;
}
