#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int n;
std::vector<std::vector<int> > ch, nb;
std::vector<int> par, v_lvl;
std::vector<std::vector<int> > lvl;
int main() {
  int en;
  scanf("%d %d", &n, &en);
  if (en == n) {
    return 0;
  }
  v_lvl.resize(n);
  par.resize(n);
  ch.resize(n);
  nb.resize(n);
  lvl.resize(n + 1);
  int m = 1;
  lvl[1].push_back(0);
  v_lvl[0] = 1;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    nb[u].push_back(v);
    nb[v].push_back(u);
  }
  int l = 2;
  while (m < n) {
    for (int k = 0; k < lvl[l - 1].size(); k++) {
      int v = lvl[l - 1][k];
      for (int h = 0; h < nb[v].size(); h++) {
        if (v_lvl[nb[v][h]] == 0) {
          v_lvl[nb[v][h]] = l;
          lvl[l].push_back(nb[v][h]);
          m++;
          ch[v].push_back(nb[v][h]);
          par[nb[v][h]] = v;
        }
      }
    }
    l++;
  }
  l--;
  std::vector<long long> lsize(l + 1, 0);
  for (int i = 1; i <= l; i++) lsize[i] = lvl[i].size();
  int e = 0;
  int o = 0;
  for (int i = 1; i <= l; i += 2) e += lsize[i];
  for (int i = 2; i <= l; i += 2) o += lsize[i];
  if (e != o) {
    cout << -1;
    return 0;
  }
  std::vector<long long> tob(n, 0);
  std::vector<long long> tow(n, 0);
  long long ans = 0;
  for (int i = l; i >= 1; i--) {
    for (auto v : lvl[i]) {
      long long b = 0;
      long long w = 0;
      for (auto u : ch[v]) {
        b += tob[u];
        w += tow[u];
      }
      long long mbw = min(b, w);
      b -= mbw;
      w -= mbw;
      if (b > 0) {
        tow[v] = b - 1;
        ans += b - 1;
      } else {
        tob[v] = w + 1;
        ans += w + 1;
      }
    }
  }
  cout << ans;
}
