#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int n = 0, f = 1;
  char c;
  for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
    ;
  if (c == '-') c = getchar(), f = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - 48;
  return n * f;
}
const int maxn = 1e5 + 5;
int i, j, n, m, s[maxn], ans, t;
int k[maxn], g[maxn << 1], ne[maxn << 1], num, b;
int u, v, f[maxn], p[maxn];
int a[maxn], cnt;
bool bz[maxn], d[maxn];
void add(int x, int y) { ne[++num] = k[x], k[x] = num, g[num] = y; }
void dfs(int x, int y) {
  int i;
  bz[x] = 1, f[x] = y, d[x] = d[y] ^ 1;
  s[x] = d[x] ? 1 : -1;
  if (x == u || x == v) s[x] += t;
  for (i = k[x]; i; i = ne[i]) {
    int go = g[i];
    if (go != y) {
      if (!bz[go])
        dfs(go, x), s[x] += s[go];
      else
        b = d[x] == d[go], u = x, v = go;
    }
  }
}
int main() {
  n = read(), m = read();
  for (i = 1; i <= m; i++) {
    int x = read(), y = read();
    add(x, y), add(y, x);
  }
  dfs(1, 0);
  if ((s[1] && !b) || (s[1] & 1)) {
    puts("-1");
    return 0;
  }
  if (n == m - 1) {
    for (i = 1; i <= n; i++) ans += abs(s[i]);
    printf("%d\n", ans);
    return 0;
  }
  memset(bz, 0, sizeof(bz));
  if (b) {
    t = -(s[1] / 2);
    if (t) {
      for (; u; u = f[u]) s[u] += t;
      for (; v; v = f[v]) s[v] += t;
    }
    ans = abs(t);
    for (i = 1; i <= n; i++) ans += abs(s[i]);
    printf("%d\n", ans);
    return 0;
  }
  for (; u; u = f[u]) p[u]++;
  for (; v; v = f[v]) p[v]--;
  a[cnt = 1] = 0;
  for (i = 1; i <= n; i++)
    if (!p[i])
      ans += abs(s[i]);
    else
      a[++cnt] = (p[i] == 1) ? -s[i] : s[i];
  int mid = (cnt + 1) / 2;
  nth_element(a + 1, a + 1 + cnt, a + mid);
  for (i = 1; i <= cnt; i++) ans += abs(a[mid] - a[i]);
  printf("%d\n", ans);
}
