#include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
using namespace std;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010, LOG = 20;
long long ans;
int n, m, k, u, v, x, y, t, a, b, X, Y;
int h[MAXN], sz[MAXN][2];
pair<int, int> val[MAXN];
vector<int> G[MAXN];
inline pair<int, int> operator+(pair<int, int> i, pair<int, int> j) {
  return {i.first + j.first, i.second + j.second};
}
void dfs1(int node, int par) {
  h[node] = h[par] + 1;
  for (int v : G[node])
    if (v != par) {
      if (!h[v])
        dfs1(v, node);
      else {
        X = v;
        Y = node;
      }
    }
}
void dfs2(int node, int par) {
  sz[node][h[node] & 1]++;
  for (int v : G[node])
    if (v != par) {
      dfs2(v, node);
      sz[node][0] += sz[v][0];
      sz[node][1] += sz[v][1];
    }
}
void dfs3(int node, int par) {
  if (h[node] & 1)
    val[node].first++;
  else
    val[node].first--;
  for (int v : G[node])
    if (v != par) {
      dfs3(v, node);
      val[node] = val[node] + val[v];
    }
}
int Solve1() {
  dfs2(1, 0);
  if (sz[1][0] != sz[1][1]) return cout << -1 << '\n', 0;
  for (int i = 2; i <= n; i++) ans += abs(sz[i][0] - sz[i][1]);
  return cout << ans << '\n', 0;
}
int Solve2() {
  int diff = sz[1][1] - sz[1][0];
  if (diff & 1) return cout << -1 << '\n', 0;
  diff >>= 1;
  if (h[X] & 1) diff *= -1;
  memset(sz, 0, sizeof(sz));
  sz[X][h[X] & 1] += diff;
  sz[Y][h[Y] & 1] += diff;
  dfs2(1, 0);
  for (int i = 2; i <= n; i++) ans += abs(sz[i][0] - sz[i][1]);
  ans += abs(diff);
  return cout << ans << '\n', 0;
}
int Solve3() {
  if (sz[1][0] != sz[1][1]) return cout << -1 << '\n', 0;
  if (h[X] % 2 == 0) swap(X, Y);
  val[X].second--;
  val[Y].second++;
  dfs3(1, 0);
  vector<int> vec = {0};
  for (int i = 1; i <= n; i++)
    if (val[i].second) {
      int tmp = val[i].first;
      vec.push_back(tmp);
    }
  sort(vec.begin(), vec.end());
  int diff = -vec[vec.size() / 2];
  memset(sz, 0, sizeof(sz));
  sz[X][h[X] & 1] += diff;
  sz[Y][h[Y] & 1] -= diff;
  dfs2(1, 0);
  for (int i = 2; i <= n; i++) ans += abs(sz[i][0] - sz[i][1]);
  ans += abs(diff);
  return cout << ans << '\n', 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs1(1, 0);
  if (m == n - 1)
    Solve1();
  else {
    cerr << "{"
         << "X"
         << ", "
         << "Y"
         << "} = {" << (X) << ", " << (Y) << "}" << endl;
    for (int &x : G[Y])
      if (x == X) swap(G[Y].back(), x);
    for (int &y : G[X])
      if (y == Y) swap(G[X].back(), y);
    G[X].pop_back();
    G[Y].pop_back();
    dfs2(1, 0);
    if (h[X] % 2 == h[Y] % 2)
      Solve2();
    else
      Solve3();
  }
  return 0;
}
