#include <bits/stdc++.h>
const int N = 1e5 + 54;
int n, m;
namespace Tree {
struct node {
  int u, v;
} edge[N << 1];
int head[N], v[N], col[N], cnt;
long long sum, ans;
inline void add(int x, int y) {
  edge[cnt].u = head[x], edge[cnt].v = y, head[x] = cnt++;
}
void dfs(int x, int fa) {
  int i;
  for (i = head[x]; ~i; i = edge[i].u) {
    int y = edge[i].v;
    if (y == fa) continue;
    col[y] = 1 - col[x], dfs(y, x);
  }
}
void solve(int x, int fa) {
  int i;
  for (i = head[x]; ~i; i = edge[i].u) {
    int y = edge[i].v;
    if (y == fa) continue;
    solve(y, x);
    v[x] += v[y];
  }
}
void SOLVE() {
  int x, y, i;
  memset(head, -1, sizeof head);
  for (i = 1; i <= m; ++i) scanf("%d%d", &x, &y), add(x, y), add(y, x);
  col[1] = 1, dfs(1, 0);
  for (i = 1; i <= n; ++i) {
    if (col[i])
      v[i] = 1;
    else
      v[i] = -1;
    sum += v[i];
  }
  if (sum) {
    puts("-1");
    return;
  }
  solve(1, 0);
  for (i = 1; i <= n; ++i) ans += abs(v[i]);
  printf("%lld\n", ans);
}
}  // namespace Tree
namespace BRT {
struct Edge {
  int id, x, y;
} E;
struct node {
  int u, v;
} edge[N << 1];
int head[N], v[N], col[N], cnt, tot, p[N], vis[N], f[N];
long long sum, ans;
inline void add(int x, int y) {
  edge[cnt].u = head[x], edge[cnt].v = y, head[x] = cnt++;
}
void dfs(int x, int fa) {
  int i;
  vis[x] = 1;
  for (i = head[x]; ~i; i = edge[i].u) {
    int y = edge[i].v;
    if (y == fa) continue;
    if (vis[y])
      E.id = i, E.x = x, E.y = y;
    else
      col[y] = 1 - col[x], dfs(y, x);
  }
}
void solve(int x, int fa) {
  int i;
  for (i = head[x]; ~i; i = edge[i].u) {
    int y = edge[i].v;
    if (y == fa || i == E.id || (i ^ 1) == E.id) continue;
    solve(y, x), v[x] += v[y], f[x] += f[y];
  }
}
void SOLVE() {
  memset(head, -1, sizeof head);
  int x, y, i;
  for (i = 1; i <= m; ++i) scanf("%d%d", &x, &y), add(x, y), add(y, x);
  col[1] = 1, dfs(1, 0);
  for (i = 1; i <= n; ++i) {
    if (col[i])
      v[i] = 1;
    else
      v[i] = -1;
    sum += v[i];
  }
  if (col[E.x] == col[E.y]) {
    if (abs(sum) & 1) {
      puts("-1");
      return;
    }
    v[E.x] -= sum / 2, v[E.y] -= sum / 2, ans += abs(sum / 2);
  } else {
    if (sum) {
      puts("-1");
      return;
    }
    f[E.x] = 1, f[E.y] = -1;
  }
  solve(1, 0);
  if (col[E.x] == col[E.y]) {
    for (i = 1; i <= n; ++i) ans += abs(v[i]);
    printf("%lld\n", ans);
    return;
  }
  for (i = 1; i <= n; ++i)
    if (f[i] == 0)
      ans += abs(v[i]);
    else
      p[++tot] = f[i] * v[i];
  p[++tot] = 0;
  std::sort(p + 1, p + tot + 1);
  int mid = p[(tot + 1) >> 1];
  for (i = 1; i <= tot; ++i) ans += abs(p[i] - mid);
  printf("%lld\n", ans);
}
}  // namespace BRT
int main() {
  freopen("b.in", "r", stdin);
  freopen("b.out", "w", stdout);
  scanf("%d%d", &n, &m);
  if (m == n - 1)
    Tree::SOLVE();
  else
    BRT::SOLVE();
}
