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