#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
const int N = 1e5, K = 2;
int n;
vi adj[N];
int color[N], n_color[K];
int s, t;
void paint(int u = 0, int p = -1, int cur_color = 1) {
  color[u] = cur_color;
  n_color[cur_color == 1]++;
  int next_color = -1 * cur_color;
  for (int v : adj[u])
    if (v != p) {
      if (!color[v])
        paint(v, u, next_color);
      else
        s = u, t = v;
    }
}
int need[N];
int calc_needs(int u = 0, int p = -1) {
  need[u] = color[u];
  for (int v : adj[u])
    if (v != p) {
      if ((u == s && v == t) || (u == t && v == s)) continue;
      need[u] += calc_needs(v, u);
    }
  return need[u];
}
bool in_cycle[N];
bool find_cycle(int u = s, int p = -1) {
  if (u == t) in_cycle[u] = true;
  for (int v : adj[u])
    if (v != p) {
      if ((u == s && v == t) || (u == t && v == s)) continue;
      if (find_cycle(v, u)) in_cycle[u] = true;
    }
  return in_cycle[u];
}
ll calc_cost() {
  ll cost = 0;
  for (int u = int(0); u < int(n); u++) cost += abs(need[u]);
  return cost;
}
int median(vi &v) {
  int k = int((v).size()) / 2;
  return (v[k] + v[k - 1]) / 2;
}
void no() {
  cout << -1 << '\n';
  exit(0);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int m;
  cin >> n >> m;
  for (int _ = int(0); _ < int(m); _++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v), adj[v].emplace_back(u);
  }
  paint();
  int dif_colors = n_color[1] - n_color[0];
  ll ans;
  if (m == n - 1) {
    if (dif_colors != 0) no();
    calc_needs();
    ans = calc_cost();
  } else if (color[s] == color[t]) {
    if (abs(dif_colors) % 2 == 1) no();
    int need_add = dif_colors / 2;
    color[s] -= need_add, color[t] -= need_add;
    calc_needs();
    ans = abs(need_add) + calc_cost();
  } else {
    if (dif_colors != 0) no();
    calc_needs();
    find_cycle();
    vi cycle_needs;
    cycle_needs.emplace_back(0);
    for (int u = int(0); u < int(n); u++)
      if (in_cycle[u] && u != s) cycle_needs.emplace_back(need[u]);
    int opt_move = median(cycle_needs);
    color[s] += opt_move, color[t] -= opt_move;
    calc_needs();
    ans = abs(opt_move) + calc_cost();
  }
  cout << ans << '\n';
  return 0;
}
