#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
pair<int, int> e[MAXN];
vector<int> adj[MAXN];
int c[MAXN];
int edge;
void dfs1(int v, int color) {
  if (c[v]) return;
  c[v] = color;
  for (auto i : adj[v]) dfs1(e[i].first + e[i].second - v, -color);
}
pair<int, int> dfs2(int v, int par) {
  pair<int, int> res;
  for (auto i : adj[v])
    if (i != par && i != edge) {
      auto now = dfs2(e[i].first + e[i].second - v, i);
      res.first += now.first + abs(now.second);
      res.second += now.second;
    }
  res.second += c[v];
  return res;
}
bitset<MAXN> b;
int find(int v, int par) {
  if (b[v]) return par;
  b[v] = true;
  for (auto i : adj[v])
    if (i != par) {
      auto res = find(e[i].first + e[i].second - v, i);
      if (res != -1) return res;
    }
  return -1;
}
vector<int> vctr;
pair<bool, pair<int, int>> dfs3(int v, int par, int node) {
  pair<int, int> res;
  bool k = (v == node);
  for (auto i : adj[v])
    if (i != par && i != edge) {
      auto now1 = dfs3(e[i].first + e[i].second - v, i, node);
      auto now = now1.second;
      k |= now1.first;
      if (now1.first) vctr.push_back(now.second);
      res.first += now.first + abs(now.second);
      res.second += now.second;
    }
  res.second += c[v];
  return {k, res};
}
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m, a1, a2;
  edge = -1;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a1 >> a2;
    e[i] = {a1, a2};
    adj[a1].push_back(i);
    adj[a2].push_back(i);
  }
  dfs1(1, 1);
  if (m == n - 1) {
    auto ans = dfs2(1, -1);
    cout << (ans.second ? -1 : ans.first);
    return 0;
  }
  edge = find(1, -1);
  if (c[e[edge].first] == c[e[edge].second]) {
    int delta = 0;
    for (int i = 1; i <= n; ++i) delta += c[i];
    if (delta & 1) return cout << -1, 0;
    c[e[edge].first] -= delta / 2;
    c[e[edge].second] -= delta / 2;
    cout << dfs2(1, -1).first + abs(delta / 2);
  } else {
    int delta = 0;
    for (int i = 1; i <= n; ++i) delta += c[i];
    if (delta) return cout << -1, 0;
    dfs3(e[edge].first, -1, e[edge].second);
    int sz = vctr.size();
    int add;
    if (sz & 1)
      add = vctr[sz / 2];
    else if (abs(vctr[sz / 2]) < abs(vctr[sz / 2 - 1]))
      add = vctr[sz / 2];
    else
      add = vctr[sz / 2 - 1];
    c[e[edge].second] -= add;
    cout << dfs2(e[edge].first, -1).first + abs(add);
  }
  return 0;
}
