#include <bits/stdc++.h>
using namespace std;
int n, m, f[100010], a[100010], fa[100010], u, v, b[100010], bk;
int cnt, head[100010], to[200010], nxt[200010];
long long ans;
bool vis[100010];
void add(int x, int y) {
  cnt++;
  to[cnt] = y;
  nxt[cnt] = head[x];
  head[x] = cnt;
}
void dfs1(int x, int c) {
  f[x] = c;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y == fa[x]) {
      continue;
    }
    fa[y] = x;
    dfs1(y, -c);
    f[x] += f[y];
  }
  ans += abs(f[x]);
}
void solve1() {
  dfs1(1, 1);
  if (f[1]) {
    puts("-1");
  } else {
    cout << ans << endl;
  }
}
void dfs2(int x) {
  f[x] = a[x];
  vis[x] = true;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y == fa[x]) {
      continue;
    }
    if (vis[y]) {
      u = x;
      v = y;
      continue;
    }
    a[y] = -a[x];
    fa[y] = x;
    dfs2(y);
    f[x] += f[y];
  }
}
void solve21() {
  if (f[1] % 2) {
    puts("-1");
    return;
  }
  int x = -f[1] / 2;
  ans = abs(x);
  int k;
  k = u;
  while (k) {
    f[k] += x;
    k = fa[k];
  }
  k = v;
  while (k) {
    f[k] += x;
    k = fa[k];
  }
  for (int i = 1; i <= n; i++) {
    ans += abs(f[i]);
  }
  cout << ans << endl;
}
void solve22() {
  if (f[1]) {
    puts("-1");
    return;
  }
  memset(vis, false, sizeof(vis));
  int k;
  k = u;
  while (k) {
    vis[k] = true;
    k = fa[k];
  }
  int g;
  k = v;
  while (k) {
    if (vis[k]) {
      g = k;
    }
    k = fa[k];
  }
  memset(vis, false, sizeof(vis));
  k = u;
  while (k != g) {
    vis[k] = true;
    b[bk++] = f[k];
    k = fa[k];
  }
  k = v;
  while (k != g) {
    vis[k] = true;
    b[bk++] = -f[k];
    k = fa[k];
  }
  b[bk++] = 0;
  sort(b, b + bk);
  int x = b[bk / 2 - 1];
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ans += abs(f[i]);
    }
  }
  for (int i = 0; i < bk; i++) {
    ans += abs(b[i] - x);
  }
  cout << ans << endl;
}
void solve2() {
  a[1] = 1;
  dfs2(1);
  if (a[u] == a[v]) {
    solve21();
  } else {
    solve22();
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  if (n == m + 1) {
    solve1();
  } else {
    solve2();
  }
  return 0;
}
