#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
vector<int> G[N];
int delv, delu;
int n, m, x, y, d[N], fa[N], dp[N], q[N];
void dfs(int x, int fx) {
  d[x] = d[fx] + 1;
  fa[x] = fx;
  dp[x] = d[x] % 2 ? 1 : -1;
  for (int y : G[x]) {
    if (y == fx) continue;
    if (d[y]) {
      delv = x;
      delu = y;
      continue;
    }
    dfs(y, x);
    dp[x] += dp[y];
  }
}
void SolveTree() {
  if (dp[1]) {
    puts("-1");
    return;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) ans += abs(dp[i]);
  printf("%d\n", ans);
}
void SolveOdd() {
  if (dp[1] % 2) {
    puts("-1");
    return;
  }
  int ans = 0, t = -dp[1] / 2;
  ans = abs(t);
  for (; delu; delu = fa[delu]) dp[delu] += t;
  for (; delv; delv = fa[delv]) dp[delv] += t;
  for (int i = 1; i <= n; i++) ans += abs(dp[i]);
  printf("%d\n", ans);
}
void SolveEven() {
  if (dp[1]) {
    puts("-1");
    return;
  }
  int ans, top = 0, mid;
  for (; delv != delu; delv = fa[delv]) {
    q[++top] = dp[delv];
    d[delv] = 0;
  }
  sort(q + 1, q + top + 1);
  mid = q[(top + 1) / 2];
  ans = abs(mid);
  for (int i = 1; i <= n; i++)
    if (d[i]) ans += abs(dp[i]);
  for (int i = 1; i <= top; i++) ans += abs(q[i] - mid);
  printf("%d\n", ans);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, 0);
  if (m == n - 1)
    SolveTree();
  else if ((d[delv] - d[delu]) % 2)
    SolveEven();
  else
    SolveOdd();
}
