#include <bits/stdc++.h>
using namespace std;
long long n, m, head[100005], nxt[200005], pnt[200005], E = 0, sz[100005], s, t,
                                                        ans = 0, k[100005];
bool odd = 0;
std::vector<long long> v;
void add_edge(long long u, long long v) {
  pnt[E] = v;
  nxt[E] = head[u];
  head[u] = E++;
}
void dfs1(long long u, long long fa) {
  for (long long i = head[u]; i != -1; i = nxt[i]) {
    long long v = pnt[i];
    if (v == fa) continue;
    if (sz[v]) {
      if (sz[v] == sz[u]) odd = 1;
      s = u;
      t = v;
    } else {
      sz[v] = -sz[u];
      dfs1(v, u);
    }
  }
}
void dfs2(long long u, long long fa) {
  for (long long i = head[u]; i != -1; i = nxt[i]) {
    long long v = pnt[i];
    if (v == fa) continue;
    if (v == s && u == t) continue;
    if (v == t && u == s) continue;
    dfs2(v, u);
    sz[u] += sz[v];
    k[u] += k[v];
  }
}
signed main() {
  memset(head, -1, sizeof(head));
  memset(sz, 0, sizeof(sz));
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= m; i++) {
    long long u, v;
    scanf("%lld%lld", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  sz[1] = 1;
  dfs1(1, 0);
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    sum += sz[i];
  }
  if (m == n - 1) {
    if (sum) {
      puts("-1");
      return 0;
    }
  } else {
    if (odd) {
      if (sum & 1) {
        puts("-1");
        return 0;
      }
      ans += abs(sum >> 1);
      sz[s] = sum >> 1;
      sz[t] = sum >> 1;
    } else {
      if (sum) {
        puts("-1");
        return 0;
      }
      k[s] = 1;
      k[t] = -1;
    }
  }
  dfs2(1, 0);
  for (long long i = 1; i <= n; i++) {
    if (k[i])
      v.push_back(k[i] * sz[i]);
    else
      ans += abs(sz[i]);
  }
  v.push_back(0);
  sort(v.begin(), v.end());
  long long len = v.size();
  long long mid = v[(len + 1) >> 1];
  for (long long i = 0; i < len; i++) {
    ans += abs(v[i] - mid);
  }
  printf("%lld\n", ans);
  return 0;
}
