#include <bits/stdc++.h>
using namespace std;
int read() {
  int q = 0;
  char ch = ' ';
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') q = q * 10 + ch - '0', ch = getchar();
  return q;
}
const int N = 100005;
int h[N], ne[N << 1], to[N << 1], sum[N], vis[N], k[N], b[N];
int n, m, tot, ss, tt, js, all;
long long ans;
void add(int x, int y) { to[++tot] = y, ne[tot] = h[x], h[x] = tot; }
void dfs1(int x, int las) {
  sum[x] = -sum[las], all += sum[x], vis[x] = 1;
  for (register int i = h[x]; i; i = ne[i]) {
    if (to[i] == las) continue;
    if (vis[to[i]])
      tt = to[i], ss = x;
    else
      dfs1(to[i], x);
  }
}
void dfs2(int x, int las) {
  for (register int i = h[x]; i; i = ne[i]) {
    if (to[i] == las || (x == ss && to[i] == tt) || (x == tt && to[i] == ss))
      continue;
    dfs2(to[i], x);
    sum[x] += sum[to[i]], k[x] += k[to[i]];
  }
}
void work1() {
  if (all) {
    puts("-1");
    return;
  }
  dfs2(1, 0);
  for (register int i = 1; i <= n; ++i) ans += abs(sum[i]);
  printf("%lld\n", ans);
}
void work2() {
  if (all & 1) {
    puts("-1");
    return;
  }
  sum[ss] -= all / 2, sum[tt] -= all / 2;
  ans += abs(all / 2);
  dfs2(1, 0);
  for (register int i = 1; i <= n; ++i) ans += abs(sum[i]);
  printf("%lld\n", ans);
}
void work3() {
  if (all) {
    puts("-1");
    return;
  }
  ++k[ss], --k[tt], dfs2(1, 0);
  b[++js] = 0;
  for (register int i = 1; i <= n; ++i)
    if (!k[i])
      ans += abs(sum[i]);
    else if (k[i] == 1)
      b[++js] = -sum[i];
    else
      b[++js] = sum[i];
  sort(b + 1, b + 1 + js);
  int x = b[(js + 1) / 2];
  for (register int i = 1; i <= n; ++i) ans += abs(x - b[i]);
  printf("%lld\n", ans);
}
int main() {
  int x, y;
  n = read(), m = read();
  for (register int i = 1; i <= m; ++i)
    x = read(), y = read(), add(x, y), add(y, x);
  sum[0] = -1, dfs1(1, 0);
  if (m == n - 1)
    work1();
  else if (sum[ss] == sum[tt])
    work2();
  else
    work3();
  return 0;
}
