use std::io; use std::io::prelude::*; use std::collections::VecDeque; fn main() -> () { let stdin = io::read_to_string(io::stdin()).unwrap(); let mut stdin = stdin.split_whitespace(); let stdout = io::stdout(); let mut stdout = io::BufWriter::new(stdout.lock()); let n = stdin.next().unwrap().parse::().unwrap(); let mut dist = [0; 10001]; let mut visited = [false; 10001]; let mut queue = VecDeque::new(); queue.push_back(1); visited[1] = true; while let Some(x) = queue.pop_front() { let mut p = 0; let mut num = x; while num != 0 { p += num & 1; num >>= 1; } let indxp = (x + p) as usize; let indxm: usize = (x - p) as usize; if (x + p) <= n && !visited[indxp] { visited[indxp] = true; dist[indxp] = dist[x as usize] + 1; queue.push_back(x + p); } if (x - p) > 0 && !visited[indxm] { visited[indxm] = true; dist[indxm] = dist[x as usize] + 1; queue.push_back(x - p); } } writeln!(stdout, "{}", if visited[n as usize] { dist[n as usize] + 1 } else { -1 }).unwrap_or(()); }