#include <bits/stdc++.h>
using namespace std;
int head[100050], to[100050 << 1], nxt[100050 << 1], cnt, n, m, dep[100050],
    vis[100050], siz[100050];
inline void add(int u, int v) {
  to[++cnt] = v;
  nxt[cnt] = head[u];
  head[u] = cnt;
}
void dfs(int x, int y) {
  vis[x] = 1;
  dep[x] = dep[y] + 1;
  if (dep[x] & 1) {
    siz[x] = 1;
  } else {
    siz[x] = -1;
  }
  int i;
  for (i = head[x]; i; i = nxt[i])
    if (to[i] != y && !vis[to[i]]) {
      dfs(to[i], x);
      siz[x] += siz[to[i]];
    }
}
int main() {
  scanf("%d%d", &n, &m);
  int i, x, y;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  dfs(1, 0);
  if (siz[1]) {
    puts("-1");
    return 0;
  }
  if (m == n - 1) {
    long long ans = 0;
    for (i = 1; i <= n; i++) ans += abs(siz[i]);
    printf("%lld\n", ans);
  }
}
