#include <bits/stdc++.h>
using namespace std;
int num[200010], fa[200010];
int find(int x) { return x == fa[x] ? fa[x] : find(fa[x]); }
int nxt[200010 << 1], to[200010 << 1], head[200010], cnt;
void add(int u, int v) {
  nxt[++cnt] = head[u];
  to[cnt] = v;
  head[u] = cnt;
}
int ton[200010], tot;
void dfs(int u, int d) {
  num[u] = d;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa[u]) continue;
    fa[v] = u;
    dfs(v, -d);
    num[u] += num[v];
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int ru = 1, rv = 1;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    int hx = find(u), hy = find(v);
    if (hx == hy)
      ru = u, rv = v;
    else
      fa[hx] = hy, add(u, v), add(v, u);
  }
  fa[ru] = 0;
  if (m == n - 1) {
    dfs(1, 1);
    int ans = 0;
    if (num[1]) {
      puts("-1");
      return 0;
    }
    for (int i = 1; i <= n; i++) ans += abs(num[i]);
    printf("%d\n", ans);
    return 0;
  }
  dfs(ru, 1);
  int u = rv;
  while (u) ton[++tot] = num[u], u = fa[u];
  if (tot & 1) {
    int ans = 0;
    if (num[ru] & 1) {
      puts("-1");
      return 0;
    }
    for (int u = rv; u; u = fa[u]) num[u] -= num[ru] >> 1;
    for (int i = 1; i <= n; i++) ans += abs(num[i]);
    printf("%d\n", ans);
    return 0;
  }
  if (num[ru]) {
    puts("-1");
    return 0;
  }
  sort(ton + 1, ton + tot + 1);
  for (int u = rv; u; u = fa[u]) num[u] -= ton[tot >> 1];
  int ans = 0;
  for (int i = 1; i <= n; i++) ans += abs(num[i]);
  printf("%d", ans);
  return 0;
}
