#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10;
vector<long long> g[N];
long long black[N], white[N], ans;
void dfs(long long v, long long p, long long curh) {
  if (curh & 1) {
    black[v] = 1;
  } else {
    white[v] = 1;
  }
  for (auto u : g[v]) {
    if (u != p) {
      dfs(u, v, curh + 1);
      black[v] += black[u];
      white[v] += white[u];
      ans += abs(black[u] - white[u]);
    }
  }
}
long long h[N], cycle_len, from, to, cnt_white, cnt_black;
long long tin[N], tout[N], timer;
bool anc(long long a, long long b) {
  return tin[a] <= tin[b] && tout[a] >= tout[b];
}
void check(long long v, long long p = 0, long long curh = 1) {
  h[v] = curh;
  tin[v] = ++timer;
  if (curh & 1) {
    cnt_black++;
  } else {
    cnt_white++;
  }
  for (auto u : g[v]) {
    if (u == p) {
      continue;
    }
    if (!h[u]) {
      check(u, v, curh + 1);
    } else {
      cycle_len = abs(h[v] - h[u]) + 1;
      from = v;
      to = u;
      if (h[v] > h[u]) {
        swap(from, to);
      }
    }
  }
  tout[v] = ++timer;
}
void dfs_even(long long v, long long p, long long x) {
  if (h[v] & 1) {
    black[v] = 1;
    white[v] = 0;
  } else {
    white[v] = 1;
    black[v] = 0;
  }
  for (auto u : g[v]) {
    if (u == p || (min(u, v) == min(from, to) && max(u, v) == max(from, to))) {
      continue;
    }
    dfs_even(u, v, x);
    if (anc(from, v) && anc(u, to)) {
      ans += abs(x + black[u] - white[u]);
    } else {
      ans += abs(black[u] - white[u]);
    }
    white[v] += white[u];
    black[v] += black[u];
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  if (m == n - 1) {
    dfs(1, 0, 0);
    if (white[1] == black[1]) {
      cout << ans << '\n';
    } else {
      cout << -1 << '\n';
    }
  } else {
    check(1);
    if (cycle_len % 2 == 0) {
      if (cnt_white != cnt_black) {
        cout << -1 << '\n';
        exit(0);
      }
      long long best = 1e18;
      for (long long x = -n; x <= n; x++) {
        ans = 0;
        dfs_even(1, 0, x);
        best = min(best, ans + abs(x));
      }
      cout << best << '\n';
    } else {
      assert(false);
    }
  }
}
