#include #include #include #include using namespace std; int main() { int n, k; cin >> n >> k; vector> g(n); for (int i = 0; i < n-1; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } if (n < k) { cout << -1 << endl; return 0; } vector dist(n, -1); dist[0] = 0; queue que; que.push(0); while(!que.empty()) { int v = que.front(); que.pop(); for (int u : g[v]) { if (dist[u] < 0) { dist[u] = dist[v] + 1; que.push(u); } } } sort(dist.begin(), dist.end()); long long ans = 0; for (int i = 1; i < k; i++) { ans += dist[i]; } cout << ans << endl; }