#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 41;
int n, col[N], cnt, m, h[N], cntCol[2];
long long ans;
vector<int> e[N], ver[N][2];
void dfs(int u, int p) {
  ++cntCol[col[u]];
  for (int i = 0; i < e[u].size(); ++i) {
    int to = e[u][i];
    if (to == p) {
      continue;
    }
    col[to] = col[u] ^ 1;
    h[to] = h[u] + 1;
    dfs(to, u);
  }
}
void pushMe(int me, int to) {
  while (ver[to][0].size() > 0) {
    ver[me][0].push_back(ver[to][0].back());
    ver[to][0].pop_back();
  }
  while (ver[to][1].size() > 0) {
    ver[me][1].push_back(ver[to][1].back());
    ver[to][1].pop_back();
  }
}
void dfs1(int u, int p) {
  ver[u][col[u]].push_back(h[u]);
  for (int i = 0; i < e[u].size(); ++i) {
    int to = e[u][i];
    if (to == p) {
      continue;
    }
    dfs1(to, u);
    pushMe(u, to);
  }
  while (ver[u][0].size() > 0 && ver[u][1].size() > 0) {
    int ha = ver[u][0].back();
    int hb = ver[u][1].back();
    ans += ha - h[u] + hb - h[u];
    ver[u][0].pop_back();
    ver[u][1].pop_back();
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  dfs(0, 0);
  if (cntCol[0] != cntCol[1]) {
    ans = -1;
  } else {
    dfs1(0, 0);
  }
  if (ver[0][0].size() > 0 || ver[0][1].size() > 0) {
    ans = -1;
  }
  printf("%lld\n", ans);
  return 0;
}
