#include <bits/stdc++.h>
using namespace std;
int f[100005], fa[100005][17], dep[100005], cnt = 0, b[100005], _u, _v;
vector<int> g[100005];
void dfs(int x, int v) {
  f[x] = v;
  for (int i = (1); i <= (16); ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1];
  for (auto to : g[x]) {
    if (!dep[to]) {
      fa[to][0] = x, dep[to] = dep[x] + 1;
      dfs(to, -v);
      f[x] += f[to];
    } else if (to != fa[x][0]) {
      _u = x, _v = to;
    }
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = (16); i >= (0); --i)
    if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
  if (x == y) return x;
  for (int i = (16); i >= (0); --i)
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
int dis(int x, int y) { return dep[x] + dep[y] - 2 * dep[lca(x, y)]; }
int main() {
  int n, m, u, v, ans = 10000000, mid, tmp, cnt;
  dep[1] = 1;
  scanf("%d%d", &n, &m);
  for (int i = (1); i <= (m); ++i) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v), g[v].push_back(u);
  }
  dfs(1, 1);
  if (m == n - 1) {
    ans = 0;
    if (f[1]) return puts("-1"), 0;
    for (int i = (1); i <= (n); ++i) ans += abs(f[i]);
    printf("%d\n", ans);
  } else {
    int qwq = lca(_u, _v);
    if ((dis(_u, _v) + 1) % 2 == 0) {
      if (f[1]) return puts("-1"), 0;
      tmp = 0, cnt = 0;
      for (int i = (1); i <= (n); ++i) tmp += abs(f[i]);
      ans = min(ans, tmp);
      for (int i = _u; i != qwq; i = fa[i][0])
        tmp -= abs(f[i]), b[++cnt] = f[i];
      for (int i = _v; i != qwq; i = fa[i][0])
        tmp -= abs(f[i]), b[++cnt] = -f[i];
      sort(b + 1, b + cnt + 1);
      cnt& 1 ? mid = b[(cnt + 1) / 2] : mid = (b[cnt / 2] + b[cnt / 2 + 1]) / 2;
      tmp += abs(mid);
      for (int i = _u; i != qwq; i = fa[i][0]) tmp += abs(f[i] - mid);
      for (int i = _v; i != qwq; i = fa[i][0]) tmp += abs(-f[i] - mid);
      ans = min(ans, tmp);
      tmp = 0, cnt = 0;
      for (int i = (1); i <= (n); ++i) tmp += abs(f[i]);
      ans = min(ans, tmp);
      for (int i = _u; i != qwq; i = fa[i][0])
        tmp -= abs(f[i]), b[++cnt] = -f[i];
      for (int i = _v; i != qwq; i = fa[i][0])
        tmp -= abs(f[i]), b[++cnt] = f[i];
      sort(b + 1, b + cnt + 1);
      cnt& 1 ? mid = b[(cnt + 1) / 2] : mid = (b[cnt / 2] + b[cnt / 2 + 1]) / 2;
      tmp += abs(mid);
      for (int i = _u; i != qwq; i = fa[i][0]) tmp += abs(-f[i] - mid);
      for (int i = _v; i != qwq; i = fa[i][0]) tmp += abs(f[i] - mid);
      ans = min(ans, tmp);
      printf("%d\n", ans);
    } else {
      if (f[1] % 2) return puts("-1"), 0;
      int tmp = f[1] / 2;
      ans = abs(tmp);
      for (int i = _u; i; i = fa[i][0]) f[i] -= tmp;
      for (int i = _v; i; i = fa[i][0]) f[i] -= tmp;
      for (int i = (1); i <= (n); ++i) ans += abs(f[i]);
      printf("%d\n", ans);
    }
  }
}
