fn bits(n: usize) -> usize { let mut n = n; let mut c = 0; while n > 0 { if n & 1 == 1 { c += 1; } n = n >> 1; } c } use std::io::stdin; use std::collections::VecDeque; pub fn main() { let n: usize = { let mut s = String::new(); let _ = stdin().read_line(&mut s); s.trim().parse().unwrap() }; let mut flags = vec![false; n + 1]; let mut answer = -1; let mut v = VecDeque::<(usize, i32)>::new(); v.push_back((1, 1)); while let Some((pos, dist)) = v.pop_front() { if pos == n { answer = dist; break; } let mv = bits(pos); let (left, right) = (pos - mv, pos + mv); if right <= n && flags[right] == false { v.push_back((right, dist + 1)); flags[right] = true; } if left > 1 && flags[left] == false { v.push_back((left, dist + 1)); flags[left] = true; } } println!("{}", answer); }