#include <bits/stdc++.h>
using namespace std;
int n, m, doe, a, b;
int now[101010], pre[202020], son[202020];
int s[101010], deep[101010];
long long ans;
void add(int x, int y) {
  ++doe;
  pre[doe] = now[x];
  now[x] = doe;
  son[doe] = y;
}
void dfs(int x, int fa) {
  deep[x] = deep[fa] + 1;
  if (deep[x] % 2 == 1)
    s[x] = 1, ++a;
  else
    s[x] = -1, ++b;
  int p = now[x];
  while (p) {
    int y = son[p];
    if (y != fa) {
      dfs(y, x);
      s[x] += s[y];
    }
    p = pre[p];
  }
  ans += abs(s[x]);
}
int main() {
  scanf("%d%d", &n, &m);
  int x, y;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  dfs(1, 0);
  if (a == b)
    printf("%lld\n", ans);
  else
    printf("-1\n");
  return 0;
}
