use std::cmp::min; use std::collections::HashSet; fn main() { let mut nk = String::new(); std::io::stdin().read_line(&mut nk).ok(); let nk: Vec = nk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nk[0]; let k = nk[1]; let mut ab: Vec> = vec![vec![]; n]; for _ in 0..n-1 { let mut aab = String::new(); std::io::stdin().read_line(&mut aab).ok(); let aab: Vec = aab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); ab[aab[0] - 1].push(aab[1] - 1); ab[aab[1] - 1].push(aab[0] - 1); } if n < k { println!("{}", -1); return; } let mut dists: Vec = vec![1000000; n]; dists[0] = 0; let mut checked: HashSet = HashSet::new(); checked.insert(0); let mut vertices: Vec = vec![]; vertices.push(0); let mut dist: usize = 1; while vertices.len() > 0 { let mut nexts: Vec = vec![]; vertices.iter().for_each(|v| { ab[*v].iter().for_each(|i| { if checked.contains(i) { return; } nexts.push(*i); checked.insert(*i); dists[*i] = min(dists[*i], dist); }); }); vertices = nexts; dist += 1; } dists.sort(); println!("{}", dists.iter().take(k).sum::()); }