#include <bits/stdc++.h>
using namespace std;
vector<int> adjList[100000];
int diff[100000];
int doDFS(int u, int p, int c) {
  int i;
  diff[u] = c;
  for (i = 0; i < adjList[u].size(); i++) {
    int v = adjList[u][i];
    if (v != p) diff[u] += doDFS(v, u, -c);
  }
  return diff[u];
}
int main() {
  int i;
  int N, M;
  int a, b;
  scanf("%d %d", &N, &M);
  for (i = 0; i < M; i++) {
    scanf("%d %d", &a, &b);
    a--, b--;
    adjList[a].push_back(b);
    adjList[b].push_back(a);
  }
  assert(M == N - 1);
  doDFS(0, -1, 1);
  if (diff[0] != 0)
    printf("-1\n");
  else {
    long long int ans = 0;
    for (i = 1; i < N; i++) ans += abs(diff[i]);
    printf("%lld\n", ans);
  }
  return 0;
}
