#include <bits/stdc++.h>
using namespace std;
int N, M, sub[100009][2], col[100009], t[100009];
long long ans;
vector<int> v[100009];
void dfs(int nod, int tata) {
  sub[nod][col[nod]] = 1;
  for (auto it = v[nod].begin(); it != v[nod].end(); it++)
    if (*it != tata)
      t[*it] = nod, col[*it] = col[nod] ^ 1, dfs(*it, nod),
      sub[nod][0] += sub[*it][0], sub[nod][1] += sub[*it][1];
  int mod = sub[nod][0] - sub[nod][1];
  if (mod < 0) mod = -mod;
  ans += mod;
}
int main() {
  scanf("%d %d\n", &N, &M);
  if (M == N) return 0;
  for (int i = 1; i < N; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, -1);
  if (sub[1][0] != sub[1][1])
    printf("-1\n");
  else
    printf("%lld\n", ans);
  return 0;
}
