#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, s = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') ch == '-' && (s = 0) || (ch = getchar());
  while (ch <= '9' && ch >= '0')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return s ? x : -x;
}
int to[100020 << 1], nxt[100020 << 1], head[100020], cnt;
void insert(int x, int y) {
  to[++cnt] = y;
  nxt[cnt] = head[x];
  head[x] = cnt;
  to[++cnt] = x;
  nxt[cnt] = head[y];
  head[y] = cnt;
}
bool dep[100020];
void dfs_deep(int x, int fa, bool d) {
  dep[x] = d;
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] == fa) continue;
    dfs_deep(to[i], x, d ^ 1);
  }
}
int s[100020], tx, ty, f[100020];
int resolve(int x, int fa) {
  s[x] = f[x];
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] == fa) continue;
    s[x] += resolve(to[i], x);
  }
  return s[x];
}
int k[100020];
int dfs(int x, int fa) {
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] == fa) continue;
    k[x] += dfs(to[i], x);
  }
  return k[x];
}
int fa[100020];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) { fa[find(x)] = find(y); }
int main(int argc, char const *argv[]) {
  freopen("D:/2_26.txt", "r", stdin);
  int n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    fa[i] = i;
  }
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read();
    if (find(x) == find(y)) {
      tx = x;
      ty = y;
    } else {
      insert(x, y);
      merge(x, y);
    }
  }
  dfs_deep(1, 0, 0);
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    f[i] = dep[i] ? 1 : -1;
    sum += f[i];
  }
  if (m == n - 1) {
    if (sum) {
      puts("-1");
    } else {
      resolve(1, 0);
      int ans = 0;
      for (int i = 1; i <= n; ++i) {
        ans += abs(s[i]);
      }
      printf("%d\n", ans);
    }
  } else {
    if (dep[tx] == dep[ty]) {
      if (abs(sum) & 1) {
        puts("-1");
      } else {
        f[tx] -= sum / 2;
        f[ty] -= sum / 2;
        int ans = abs(sum) / 2;
        resolve(1, 0);
        for (int i = 1; i <= n; ++i) {
          ans += abs(s[i]);
        }
        printf("%d\n", ans);
      }
    } else {
      resolve(1, 0);
      k[tx] = 1;
      k[ty] = -1;
      dfs(1, 0);
      vector<int> vec;
      for (int i = 1; i <= n; ++i) {
        if (k[i]) {
          vec.push_back(-k[i] * s[i]);
        }
      }
      sort(vec.begin(), vec.end());
      int mid = vec[vec.size() / 2];
      int ans = abs(mid);
      for (int i = 1; i <= n; ++i) {
        ans += abs(k[i] * mid + s[i]);
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
