#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
int const N = 1e5 + 20;
int n, m;
vector<int> g[N];
struct edge {
  int u, v;
  bool in;
} e[N];
vector<int> vec1, vec2;
int d, ans, h[N];
bool vis[N];
int dfs(int v) {
  int d = 1;
  vis[v] = 1;
  for (int i : g[v]) {
    int u = e[i].u ^ e[i].v ^ v;
    if (!vis[u]) {
      e[i].in = 1;
      h[u] = h[v] ^ 1;
      d -= dfs(u);
    }
  }
  ans += abs(d);
  return d;
}
bool fl[N];
int token[N];
int want = -1;
int get(int v) {
  vis[v] = 1;
  int d = token[v];
  for (int i : g[v]) {
    int u = e[i].u ^ e[i].v ^ v;
    if (!vis[u]) {
      d += get(u);
      fl[v] |= fl[u];
    }
  }
  if (!fl[v])
    vec1.push_back(d);
  else
    vec2.push_back(d);
  ans += abs(d);
  return d;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> e[i].u >> e[i].v;
    e[i].u--, e[i].v--;
    g[e[i].u].push_back(i);
    g[e[i].v].push_back(i);
  }
  d = dfs(0);
  if (m == n - 1) {
    if (!d)
      cout << ans << '\n';
    else
      cout << "-1\n", 0;
    return 0;
  }
  bool odd_cycle = 0;
  for (int i = 0; i < m; i++)
    if (!e[i].in && h[e[i].u] == h[e[i].v]) odd_cycle = 1;
  memset(vis, 0, sizeof vis);
  if (odd_cycle) {
    if (d & 1) return cout << "-1\n", 0;
    d /= 2;
    for (int i = 0; i < m; i++)
      if (!e[i].in)
        want = h[e[i].u], token[e[i].u] = token[e[i].v] = (want ? d : -d);
    for (int i = 0; i < n; i++) {
      if (h[i] == want)
        token[i]++;
      else
        token[i]--;
    }
    ans = abs(d);
    get(0);
    cout << ans << '\n';
  } else {
    if (d) return cout << "-1\n", 0;
    for (int i = 0; i < n; i++) token[i] = (h[i] ? +1 : -1);
    int X, Y;
    for (int i = 0; i < m; i++)
      if (!e[i].in) X = e[i].u, Y = e[i].v;
    fl[Y] = 1;
    get(X);
    ans = 0;
    for (int p : vec1) ans += abs(p);
    if (vec2.size()) {
      int x = -vec2[(int)vec2.size() / 2];
      for (int p : vec2) ans += abs(p + x);
      ans += x;
    }
    cout << ans << '\n';
  }
}
