#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
const long long inf = 50000;
long long n, m, val[maxn], d[maxn], cut, x, y, siz, first[maxn], cnt, ans;
long long f[maxn], tot, a[maxn];
bool vis[maxn];
struct Edge {
  long long to, next;
} e[maxn << 1];
void add(long long u, long long v) {
  e[cnt] = (Edge){v, first[u]};
  first[u] = cnt++;
}
void Dfs1(long long u, long long fa) {
  d[u] = d[fa] + 1;
  val[u] = (d[u] & 1) ? 1 : -1;
  vis[u] = 1;
  for (long long i = first[u]; i != -1; i = e[i].next) {
    long long v = e[i].to;
    if (v == fa) continue;
    if (vis[v]) {
      siz = d[u] + d[v] - 1;
      cut = i;
      x = u, y = v;
      continue;
    }
    Dfs1(v, u);
    val[u] += val[v];
  }
}
void Dfs2(long long u, long long fa) {
  for (long long i = first[u]; i != -1; i = e[i].next) {
    long long v = e[i].to;
    if (v == fa || i == cut || (i ^ 1) == cut) continue;
    Dfs2(v, u);
    f[u] += f[v];
  }
}
signed main() {
  cin >> n >> m;
  memset(first, -1, sizeof(first));
  for (long long i = 1; i <= m; i++) {
    long long u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  Dfs1(1, 0);
  if (m == n - 1) {
    if (val[1])
      cout << -1 << endl;
    else {
      for (long long i = 1; i <= n; i++) ans += abs(val[i]);
      cout << ans << endl;
    }
  }
  if (m == n) {
    if (siz & 1) {
      if (val[1] % 2 != 0)
        cout << -1 << endl;
      else {
        val[x] -= val[1] / 2;
        val[y] -= val[1] / 2;
        for (long long i = 1; i <= n; i++) ans += abs(val[i]);
        cout << ans << endl;
      }
    } else {
      if (val[1])
        cout << -1 << endl;
      else {
        a[++tot] = 0;
        f[x] = 1, f[y] = -1;
        Dfs2(1, 0);
        for (long long i = 1; i <= n; i++) {
          if (!f[i])
            ans += abs(val[i]);
          else
            a[++tot] = -f[i] * val[i];
        }
        sort(a + 1, a + tot + 1);
        long long vd = a[(tot + 1) / 2];
        for (long long i = 1; i <= tot; i++) ans += abs(vd - a[i]);
        cout << ans << endl;
      }
    }
  }
}
