#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y, X, Y, xb, len, tot, tmp, ans, head[300000], f[300000],
    q[300000], fa[300000];
bool vis[300000];
struct edge {
  long long v, nxt;
} e[300000];
void add(long long x, long long y) {
  e[++tot].v = y;
  e[tot].nxt = head[x];
  head[x] = tot;
}
void dfs(long long u, long long o) {
  f[u] = o;
  tmp += o;
  vis[u] = 1;
  for (long long i = head[u], v; i; i = e[i].nxt)
    if (!vis[v = e[i].v]) {
      fa[v] = u;
      dfs(v, -o);
    } else if (v != fa[u]) {
      X = v;
      Y = u;
    }
}
void calc(int u) {
  for (int i = head[u], v; i; i = e[i].nxt)
    if (fa[v = e[i].v] == u) {
      calc(v);
      f[u] += f[v];
      ans += abs(f[v]);
    }
}
int main() {
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= m; ++i) {
    scanf("%lld%lld", &x, &y);
    add(x, y);
    add(y, x);
  }
  dfs(1, 1);
  if (m < n) {
    calc(1);
    printf("%lld\n", f[1] ? -1 : ans);
    return 0;
  }
  len = 1;
  for (int i = X; i != Y; i = fa[i]) ++len;
  if (tmp & 1) {
    puts("-1");
    return 0;
  }
  if (len & 1) {
    f[X] -= tmp / 2;
    f[Y] -= tmp / 2;
    ans += abs(tmp / 2);
    calc(1);
    printf("%lld\n", ans);
    return 0;
  }
  calc(1);
  q[++xb] = 0;
  for (int i = X; i != Y; i = fa[i]) {
    q[++xb] = f[i];
    ans -= abs(f[i]);
  }
  sort(q + 1, q + xb + 1);
  for (int i = 1; i <= xb / 2; ++i) ans += q[xb - i + 1] - q[i];
  printf("%lld\n", ans);
  return 0;
}
