#include <bits/stdc++.h>
using namespace std;
const int M = 5e5 + 10, N = 2e6 + 10;
const int mod = 1e9 + 7;
int n, m;
int head[M], nxt[M], to[M], deg[M], tot = 0;
bool del[M];
inline void add(int u, int v) {
  ++tot;
  nxt[tot] = head[u];
  head[u] = tot;
  to[tot] = v;
}
inline void adde(int u, int v) {
  add(u, v);
  add(v, u);
  deg[u]++, deg[v]++;
}
inline int Abs(int x) { return x < 0 ? -x : x; }
namespace solveCycleTree {
long long w[M], f[M], g[M], tem[M];
int tn, cir1, cir2, odd;
inline void dfs_w(int x, int fa) {
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y == fa) continue;
    if (w[y] == 0) {
      w[y] = -w[x];
      dfs_w(y, x);
    } else {
      cir1 = x, cir2 = y;
      odd = (w[x] == w[y]);
    }
  }
}
inline void dp(int x, int fa) {
  f[x] = w[x];
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] == fa || (to[i] == cir1 && x == cir2) ||
        (to[i] == cir2 && x == cir1))
      continue;
    dp(to[i], x);
    f[x] = f[x] + f[to[i]];
    g[x] = g[x] + g[to[i]];
  }
}
inline void treeMain() {
  cir1 = cir2 = -1;
  odd = -1;
  w[1] = 1;
  dfs_w(1, 0);
  if (f[1] != 0) {
    puts("-1");
    return;
  }
  dp(1, 0);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) ans += Abs(f[i]);
  printf("%lld\n", ans);
}
inline void main() {
  long long ans = 0;
  cir1 = cir2 = -1;
  odd = -1;
  w[1] = 1;
  dfs_w(1, 0);
  if (odd) {
    dp(1, 0);
    if (f[1] & 1) {
      puts("-1");
      return;
    }
    int t = f[1];
    w[cir1] -= t / 2, w[cir2] -= t / 2;
    ans += Abs(t / 2);
    dp(1, 0);
    for (int i = 1; i <= n; ++i) ans += Abs(f[i]);
    printf("%lld\n", ans);
  } else {
    memset(g, 0, sizeof g);
    g[cir1]++, g[cir2]--;
    dp(1, 0);
    if (f[1] != 0) {
      puts("-1");
      return;
    }
    tn = 0;
    for (int i = 1; i <= n; ++i) {
      if (g[i] == 0) {
        ans = ans + Abs(f[i]);
        continue;
      }
      if (g[i] == 1)
        tem[++tn] = -f[i];
      else
        tem[++tn] = f[i];
    }
    sort(tem + 1, tem + tn + 1);
    int ansid = tem[(1 + tn) >> 1];
    for (int i = 1; i <= tn; ++i) ans = ans + Abs(ansid - tem[i]);
    printf("%lld\n", ans);
  }
}
}  // namespace solveCycleTree
inline void sol() {
  tot = 0;
  memset(head, 0, sizeof head);
  memset(del, 0, sizeof del);
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    adde(u, v);
  }
  bool taskTree = (m + 1 == n);
  bool taskCycleTree = (m == n);
  if (taskTree) {
    solveCycleTree::treeMain();
    return;
  }
  solveCycleTree::main();
}
int main() {
  sol();
  return 0;
}
