#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y, X, Y, len, tot, tmp, head[300000], f[300000], g[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] = 0;
  g[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] + abs(g[v]);
      g[u] += g[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", g[1] ? -1 : f[1]);
    return 0;
  }
  len = 1;
  for (int i = X; i != Y; i = fa[i]) ++len;
  if (len & 1) {
    if (tmp & 1) {
      puts("-1");
      return 0;
    }
    g[X] -= tmp / 2;
    g[Y] -= tmp / 2;
    f[1] += abs(tmp / 2);
    calc(1);
    printf("%lld\n", f[1]);
    return 0;
  }
  printf("%lld\n", g[1] ? -1 : f[1]);
  return 0;
}
