#include <bits/stdc++.h>
using namespace std;
long long n, m, ans, val[100005], ban[100005];
vector<long long> g[100005];
long long dfs(long long x, long long p, long long dep) {
  long long got = 0;
  for (auto to : g[x])
    if (to != p && !ban[to] && to != -1) {
      long long v = dfs(to, x, dep ^ 1);
      ans += abs(v);
      got += v;
    }
  if (dep & 1)
    got -= val[x];
  else
    got += val[x];
  return got;
}
void solve_tree() {
  if (dfs(1, -1, 0)) {
    cout << -1 << endl;
    return;
  }
  cout << ans << endl;
}
vector<long long> cyc;
bool vis[100005];
long long par[100005], col[100005], cnt[2];
void go(long long x, long long p, long long dep) {
  par[x] = p;
  cnt[dep & 1]++;
  col[x] = dep & 1;
  vis[x] = 1;
  for (auto to : g[x]) {
    if (to != p) {
      if (vis[to]) {
        if (cyc.size()) continue;
        long long cur = x;
        while (cur != to) {
          cyc.push_back(cur);
          cur = par[cur];
        }
        cyc.push_back(cur);
      } else {
        go(to, x, dep + 1);
      }
    }
  }
}
signed main() {
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (long long i = 1; i <= n; i++) val[i] = 1;
  if (m == n - 1) {
    solve_tree();
    return 0;
  } else {
    go(1, 0, 0);
    if (cyc.size() % 2 == 0) {
      for (auto vv : cyc) ban[vv] = 1;
      vector<long long> dat;
      for (auto vv : cyc) {
        dat.push_back(dfs(vv, 0, col[vv]));
      }
      sort(dat.begin(), dat.end());
      for (long long i = 1; i < dat.size(); i++) dat[i] += dat[i - 1];
      if (dat.back()) {
        puts("-1");
        return 0;
      }
      for (long long i = 0; i < dat.size() / 2; i++) ans -= dat[i];
      for (long long i = dat.size() / 2; i < dat.size(); i++) ans += dat[i];
      cout << ans << endl;
    } else {
      long long x = cyc[0], y = cyc.back();
      long long dif = cnt[col[x]] - cnt[1 - col[x]];
      if (dif % 2) {
        puts("-1");
        return 0;
      }
      val[x] -= dif / 2;
      val[y] -= dif / 2;
      ans += abs(dif / 2);
      for (auto &v : g[x])
        if (v == y) v = -1;
      for (auto &v : g[y])
        if (v == x) v = -1;
      if (dfs(1, -1, 0)) {
        puts("-1");
        return 0;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
