use std::collections::VecDeque; fn main() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let n: usize = s.trim().parse().unwrap(); let (mut d, mut q) = (vec![-1; n + 1], VecDeque::from([1usize])); d[1] = 1; while !q.is_empty() { let i = q.pop_front().unwrap(); let c = i.count_ones() as usize; if i + c <= n && d[i + c] == -1 { d[i + c] = d[i] + 1; q.push_back(i + c) } if d[i - c] == -1 { d[i - c] = d[i] + 1; q.push_back(i - c) } } println!("{}", d[n]) }