use std::collections::VecDeque; fn main() { input!{ n: usize, } let mut masu = vec![1_000_000_usize; n + 1]; masu[1] = 1; let mut que = VecDeque::::new(); que.push_back(1); while let Some(now) = que.pop_front() { let ones = count_up_one(now); let now_hop = masu[now]; if now >= 1 + ones { //now - ones >= 1 let left_next = now - ones; if now_hop + 1 < masu[left_next] { masu[left_next] = now_hop + 1; que.push_back(left_next); } } let right_next = now + ones; if right_next <= n { if now_hop + 1 < masu[right_next] { masu[right_next] = now_hop + 1; que.push_back(right_next); } } } if masu[n] <= n { println!("{}", masu[n]); } else { println!("-1"); } } fn count_up_one(x: usize) -> usize { let mut res = 0; let mut x = x; while x > 0 { if (x & 1) >= 1 { res += 1; } x >>= 1; } res } #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { #[allow(unused_mut)] let mut s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; }