#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool cmin(T& a, const T& b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool cmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
void read(T& x) {
  int f = 1;
  char ch;
  x = 0;
  while (!isdigit(ch = getchar()))
    if (ch == '-') f = -1;
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
const int NX = 200010;
int n, m;
vector<int> edge[NX];
bool used[NX];
int par[NX], siz[NX], tu, tv, len;
namespace tree {
long long ans = 0;
int dfs(int x, int fa, int d) {
  int cur_size = (d & 1) ? 1 : -1;
  for (int y : edge[x]) {
    if (y != fa) {
      cur_size += dfs(y, x, d + 1);
    }
  }
  ans += abs(cur_size);
  return siz[x] = cur_size;
}
void solve() {
  ans = 0;
  if (dfs(1, 0, 1))
    puts("-1");
  else
    cout << ans << "\n";
}
}  // namespace tree
void dfs(int x) {
  used[x] = true;
  for (int y : edge[x]) {
    if (y == par[x]) continue;
    if (used[y]) {
      if (tu) continue;
      tu = x, tv = y;
      len = 1;
      for (int u = tu; u != tv; u = par[u]) ++len;
    } else {
      par[y] = x;
      dfs(y);
    }
  }
}
int get_circle() {
  dfs(1);
  for (int i = 0; i < ((int)(edge[tu]).size()); i++)
    if (edge[tu][i] == tv) edge[tu].erase(edge[tu].begin() + i);
  for (int i = 0; i < ((int)(edge[tv]).size()); i++)
    if (edge[tv][i] == tu) edge[tv].erase(edge[tv].begin() + i);
  return len;
}
namespace odd {
long long ans = 0;
int sym[NX];
int dfs(int x, int fa, int d) {
  int cur_size = (d & 1) ? 1 : -1;
  if (x == tu || x == tv) cur_size += sym[x];
  for (int y : edge[x]) {
    if (y != fa) {
      cur_size += dfs(y, x, d + 1);
    }
  }
  ans += abs(cur_size);
  return siz[x] = cur_size;
}
void solve() {
  int t = dfs(1, 0, 1);
  if (t & 1)
    puts("-1");
  else {
    ans = (abs(t) >> 1);
    sym[tu] = -(t >> 1);
    sym[tv] = -(t >> 1);
    dfs(1, 0, 1);
    cout << ans << "\n";
  }
}
}  // namespace odd
namespace even {
int ts[NX], tk[NX];
pair<int, int> dfs(int x, int fa, int d) {
  int cur_size = (d & 1) ? 1 : -1;
  int cur_k = 0;
  if (x == tu || x == tv) cur_k = tk[x];
  for (int y : edge[x]) {
    if (y != fa) {
      pair<int, int> thi = dfs(y, x, d + 1);
      cur_size += thi.first;
      cur_k += thi.second;
    }
  }
  return make_pair(ts[x] = cur_size, tk[x] = cur_k);
}
void solve() {
  pair<int, int> t = dfs(1, 0, 1);
  tk[tu] = 1;
  tk[tv] = -1;
  if (t.first)
    puts("-1");
  else {
    long long ans = 0;
    vector<int> v;
    for (int i = 1; i <= n; i++) {
      if (!tk[i])
        ans += abs(ts[i]);
      else {
        ts[i] *= -1;
        v.push_back(ts[i]);
      }
    }
    v.push_back(0);
    sort(v.begin(), v.end());
    int mid = v[((int)(v).size()) >> 1];
    for (int i : v) ans += abs(i - mid);
    cout << ans << "\n";
  }
}
}  // namespace even
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    read(u);
    read(v);
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  if (m == n - 1)
    tree::solve();
  else if (get_circle() & 1)
    odd::solve();
  else
    even::solve();
  return 0;
}
