#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int to, next;
} edge[(200000 + 1000) << 1];
int n, m, u, v, K[(200000 + 1000)], size[(200000 + 1000)], sum, cir_num;
int Depth[(200000 + 1000)], Father[(200000 + 1000)], stx, sty;
int head[(200000 + 1000)], num_edge;
void add(int u, int v) {
  edge[++num_edge].to = v;
  edge[num_edge].next = head[u];
  head[u] = num_edge;
}
void Dfs(int x, int fa) {
  Depth[x] = Depth[fa] + 1;
  Father[x] = fa;
  size[x] = (Depth[x] % 2 == 1) ? 1 : -1;
  for (int i = head[x]; i; i = edge[i].next)
    if (edge[i].to != fa) {
      if (Depth[edge[i].to]) {
        if (cir_num) continue;
        stx = x;
        sty = edge[i].to;
        cir_num = Depth[x] - Depth[edge[i].to] + 1;
        continue;
      }
      Dfs(edge[i].to, x);
      size[x] += size[edge[i].to];
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  Dfs(1, 0);
  if (m == n - 1) {
    if (size[1] != 0)
      printf("-1");
    else {
      long long ans = 0;
      for (int i = 1; i <= n; ++i) ans += abs(size[i]);
      printf("%lld", ans);
    }
  } else if (cir_num % 2 == 1) {
    if (abs(size[1]) % 2 == 1)
      printf("-1");
    else {
      long long ans = abs(size[1]) / 2;
      for (int u = stx; u; u = Father[u]) size[u] -= size[1] / 2;
      for (int u = sty; u; u = Father[u]) size[u] -= size[1] / 2;
      for (int i = 1; i <= n; ++i) ans += abs(size[i]);
      printf("%lld", ans);
    }
  } else {
    puts("GG");
    return 0;
    if (size[1] != 0)
      printf("-1");
    else {
    }
  }
}
