use std::collections::HashSet; use std::io::{self, Read}; fn read_stdin() -> Vec { let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer).ok(); buffer.trim().split('\n').map(|s| s.to_string()).collect() } fn main() { let input = read_stdin(); let n = *&input[0].parse::().unwrap(); let mut pos: i16 = 1; let mut memo = HashSet::new(); let mut step = 1; memo.insert(pos); while n != pos { let x: i16 = format!("{:b}", pos).chars().filter(|c| c == &'1').count() as i16; memo.insert(pos); if pos + x <= n { pos += x; } else { pos -= x; if pos <= 0 { println!("-1"); return; } } if memo.contains(&pos) { println!("-1"); return; } step += 1; } println!("{}", step); }