use std::{ collections::HashSet, io::{self, BufRead}, }; fn main() { io::stdin().lock().lines().for_each(|l| { let goal = l.unwrap().parse::().unwrap(); if goal == 1 { println!("1"); return; } let pop_counts = (0..=goal) .map(|n| n.count_ones() as usize) .collect::>(); let mut positions = HashSet::new(); positions.insert(1); let mut reached = positions.clone(); for i in 2.. { let mut new_positions = HashSet::new(); for p in &positions { let left = p - pop_counts[*p]; if left >= 1 { new_positions.insert(left); } let right = p + pop_counts[*p]; if right <= goal { new_positions.insert(right); } } if new_positions.contains(&goal) { println!("{}", i); break; } else if new_positions.iter().all(|p| reached.contains(p)) { println!("-1"); break; } positions = new_positions.clone(); reached.extend(new_positions); } }); }