#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> edge[100005];
long long sum[100005][2];
int C[100005];
void dfs(int v, int u, int col) {
  sum[v][0] = col;
  sum[v][1] = 1 - col;
  C[v] = col;
  for (int i = 0; i < edge[v].size(); i++) {
    if (edge[v][i] == u) continue;
    dfs(edge[v][i], v, 1 - col);
    for (int j = 0; j < 2; j++) sum[v][j] += sum[edge[v][i]][j];
  }
}
void sol_tree() {
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  dfs(1, -1, 0);
  if (sum[1][0] % 2 != sum[1][1] % 2) {
    cout << -1 << endl;
    exit(0);
  }
  long long ans = 0;
  for (int i = 2; i <= n; i++) ans += abs(sum[i][0] - sum[i][1]);
  cout << ans << endl;
}
int par[100005], ran[100005];
void init() {
  for (int i = 0; i < 100005; i++) par[i] = i;
}
int find(int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (ran[x] < ran[y])
    par[x] = y;
  else {
    par[y] = x;
    if (ran[x] == ran[y]) ran[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int X, Y;
long long cur_param;
long long S[100005], summ;
long long dfss(int v, int u, int col) {
  S[v] = C[v];
  if (v == X) S[v] += cur_param;
  if (v == Y) S[v] -= cur_param;
  long long target = 1 - C[v];
  for (int i = 0; i < edge[v].size(); i++) {
    if (edge[v][i] == u) continue;
    target += dfss(edge[v][i], v, 1 - col);
    S[v] += S[edge[v][i]];
  }
  if (v != 1) summ += abs(target - S[v]);
  return target;
}
long long dfsss(int v, int u, int col) {
  S[v] = C[v];
  if (v == X) S[v] += cur_param;
  if (v == Y) S[v] += cur_param;
  long long target = 1 - C[v];
  for (int i = 0; i < edge[v].size(); i++) {
    if (edge[v][i] == u) continue;
    target += dfsss(edge[v][i], v, 1 - col);
    S[v] += S[edge[v][i]];
  }
  if (v != 1) summ += abs(target - S[v]);
  return target;
}
void sol_namori() {
  init();
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    if (!same(a, b)) {
      edge[a].push_back(b);
      edge[b].push_back(a);
    } else {
      X = a;
      Y = b;
    }
    unite(a, b);
  }
  dfs(1, -1, 0);
  if (C[X] != C[Y]) {
    if (sum[1][0] != sum[1][1]) {
      puts("-1");
      exit(0);
    }
    long long lb = -1e9, ub = 1e9;
    while (ub - lb > 2) {
      long long x = (lb * 2 + ub) / 3;
      long long y = (lb + ub * 2) / 3;
      cur_param = x;
      summ = 0;
      dfss(1, -1, 0);
      long long zip_x = summ;
      cur_param = y;
      summ = 0;
      dfss(1, -1, 0);
      long long zip_y = summ;
      if (zip_x < zip_y) {
        ub = y;
      } else {
        lb = x;
      }
    }
    long long ans = 1e18;
    for (long long x = lb - 5; x <= lb + 5; x++) {
      cur_param = x;
      summ = 0;
      dfss(1, -1, 0);
      ans = min(ans, summ);
    }
    cout << ans << endl;
  } else {
    if (sum[1][0] % 2 != sum[1][1] % 2) {
      puts("-1");
      exit(0);
    }
    int x = sum[1][1] - sum[1][0];
    x /= 2;
    cur_param = x;
    summ = 0;
    dfsss(1, -1, 0);
    cout << summ + abs(x) << endl;
  }
  return;
}
int main() {
  cin >> n >> m;
  if (m == n - 1)
    sol_tree();
  else
    sol_namori();
  return 0;
}
