#include <bits/stdc++.h>
int n, m, u, v, nex[200001], fir[100001], sto[200001], tot, fa[100001];
long long ans, f[200001][2];
void addbian(int aa, int bb) {
  tot++;
  nex[tot] = fir[aa];
  fir[aa] = tot;
  sto[tot] = bb;
}
void dfs(int x) {
  int aa = fir[x];
  f[x][1] = 1;
  f[x][0] = 0;
  while (aa != 0) {
    if (sto[aa] != fa[x]) {
      fa[sto[aa]] = x;
      dfs(sto[aa]);
      int bb = 0;
      if (f[sto[aa]][bb] == 0) bb = 1 - bb;
      if (f[sto[aa]][bb] + f[x][1 - bb] >= f[x][bb]) {
        f[x][1 - bb] = f[sto[aa]][bb] + f[x][1 - bb] - f[x][bb];
        f[x][bb] = 0;
      } else {
        f[x][bb] = f[x][bb] - f[sto[aa]][bb] - f[x][1 - bb];
        f[x][1 - bb] = 0;
      }
    }
    aa = nex[aa];
  }
  ans = ans + f[x][0] + f[x][1];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 2; i <= n; i++) {
    scanf("%d%d", &u, &v);
    addbian(u, v);
    addbian(v, u);
  }
  dfs(1);
  if ((f[1][0] == 0) && (f[1][1] == 0))
    printf("%d\n", ans);
  else
    printf("-1\n");
}
