#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int f[maxn], vis[maxn], fa[maxn], d[maxn], u, v, x, y, a[maxn], cnt, ans, n, m;
vector<int> G[maxn];
void dfs(int u) {
  f[u] = 1;
  vis[u] = 1;
  for (int v : G[u])
    if (v ^ fa[u]) {
      if (vis[v]) {
        x = u, y = v;
        continue;
      }
      fa[v] = u;
      d[v] = d[u] + 1;
      dfs(v);
      f[u] -= f[v];
    }
}
int lca(int u, int v) {
  if (d[u] < d[v]) swap(u, v);
  while (d[u] ^ d[v]) u = fa[u];
  while (u ^ v) u = fa[u], v = fa[v];
  return u;
}
int main() {
  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);
  if (m == n - 1) {
    if (f[1]) return puts("-1"), 0;
    for (int i = (1); i <= (n); ++i) ans += abs(f[i]);
  } else if (!((d[x] ^ d[y]) & 1)) {
    if (f[1] & 1) return puts("-1"), 0;
    int fxk = -(f[1] >> 1);
    ans += abs(fxk);
    for (int u = x; u; u = fa[u]) f[u] += fxk;
    for (int u = y; u; u = fa[u]) f[u] += fxk;
    for (int i = (1); i <= (n); ++i) ans += abs(f[i]);
  } else {
    if (f[1]) return puts("-1"), 0;
    int t = lca(x, y);
    for (int u = x; u ^ t; u = fa[u]) a[++cnt] = f[u], vis[u] = 0;
    for (int u = y; u ^ t; u = fa[u]) a[++cnt] = -f[u], vis[u] = 0;
    a[++cnt] = 0;
    sort(a + 1, a + cnt + 1);
    int fxk = a[cnt + 1 >> 1];
    for (int i = (1); i <= (n); ++i)
      if (vis[i]) ans += abs(f[i]);
    for (int i = (1); i <= (cnt); ++i) ans += abs(a[i] - fxk);
  }
  printf("%d\n", ans);
  return 0;
}
