#include <bits/stdc++.h>
using namespace std;
inline int inn() {
  int x, ch;
  while ((ch = getchar()) < '0' || ch > '9')
    ;
  x = ch ^ '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x;
}
inline int gabs(int x) { return x < 0 ? -x : x; }
struct edges {
  int to, pre;
} e[100010 << 1];
int h[100010], etop, find_cir, s[100010], onc[100010], vis[100010];
int val[100010], u[100010], v[100010], a[100010], d[100010], k[100010];
stack<int> stc;
inline int add_edge(int u, int v) {
  return e[++etop].to = v, e[etop].pre = h[u], h[u] = etop;
}
inline int getdpt(int x, int dpt = 0) {
  vis[x] = 1;
  for (int i = h[x], y; i; i = e[i].pre)
    if (!vis[y = e[i].to]) getdpt(y, dpt ^ 1);
  return val[x] = ((d[x] = dpt) ? 1 : -1);
}
inline int getans(int x, int ans = 0) {
  vis[x] = 1, s[x] = val[x];
  for (int i = h[x], y; i; i = e[i].pre)
    if (!vis[y = e[i].to]) ans += getans(y), s[x] += s[y], k[x] += k[y];
  return ans += gabs(s[x]);
}
int dfs(int x, int fa = 0) {
  if (find_cir) return 0;
  stc.push(x), vis[x] = 1;
  for (int i = h[x], y; i; i = e[i].pre)
    if ((y = e[i].to) ^ fa) {
      if (find_cir) return 0;
      if (!vis[y])
        dfs(y, x);
      else {
        find_cir = 1, onc[x] = onc[y] = 1;
        while (stc.top() != y) onc[stc.top()] = 1, stc.pop();
      }
    }
  return (stc.top() == x ? stc.pop(), 0 : 0), 0;
}
int main() {
  int n = inn(), m = inn(), ans = 0, c = 0, x = 0;
  for (int i = 1; i <= m; i++)
    u[i] = inn(), v[i] = inn(), add_edge(u[i], v[i]), add_edge(v[i], u[i]);
  getdpt(1), memset(vis, 0, sizeof(int) * ((n) + 1)),
      ans = getans(1), memset(vis, 0, sizeof(int) * ((n) + 1));
  if (m < n) return !printf("%d\n", s[1] ? -1 : ans);
  dfs(1), memset(vis, 0, sizeof(int) * ((n) + 1)),
      memset(h, 0, sizeof(int) * ((n) + 1)), etop = 0;
  for (int i = 1; i <= n; i++)
    if (onc[i]) c++;
  for (x = 1; !onc[u[x]] || !onc[v[x]]; x++)
    ;
  for (int i = 1; i <= m; i++)
    if (x ^ i) add_edge(u[i], v[i]), add_edge(v[i], u[i]);
  if (c & 1) {
    if (gabs(s[1]) & 1) return !printf("-1\n");
    val[u[x]] -= s[1] / 2, val[v[x]] -= s[1] / 2, ans = gabs(s[1]) / 2;
    return !printf("%d\n", ans + getans(1));
  }
  if (s[1]) return !printf("-1\n");
  k[u[x]] = 1, k[v[x]] = -1, getans(1), ans = 0, c = 0;
  for (int i = 1; i <= n; i++)
    if (!k[i])
      ans += gabs(s[i]);
    else
      a[++c] = (k[i] > 0 ? s[i] : -s[i]);
  a[++c] = 0, sort(a + 1, a + c + 1), x = a[(c + 1) / 2];
  for (int i = 1; i <= c; i++) ans += gabs(a[i] - x);
  return !printf("%d\n", ans);
}
