use proconio::input; use std::collections::*; type Map = BTreeMap; type Set = BTreeSet; type Deque = VecDeque; fn main() { input! { n: usize } let mut memo = vec![usize::MAX/2; n + 1]; memo[1] = 1; let mut que: Deque = Deque::new(); que.push_back(1); while !que.is_empty() { let v = que.pop_front().unwrap(); let ones = v.count_ones() as usize; if v + ones <= n { if memo[v + ones] > memo[v] + 1 { memo[v + ones] = memo[v] + 1; que.push_back(v + ones); } } if v > ones && v - ones <= n { if memo[v - ones] > memo[v] + 1 { memo[v - ones] = memo[v] + 1; que.push_back(v - ones); } } } if memo[n] != usize::MAX/2 { println!("{}", memo[n]); }else { println!("-1"); } }