macro_rules! scan { ($t:ty) => { { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse::<$t>().unwrap() } }; ($t:ty;;) => { { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse::<$t>().unwrap()) .collect::>() } }; ($t:ty; $n:expr) => { (0..$n).map(|_| scan!($t) ).collect::>() }; ($t:ty;;$n:expr) => { (0..$n).map(|_| scan!($t;;)).collect::>() }; ($($t:ty),*; $n:expr) => { (0..$n).map(|_| scan!($($t),*) ).collect::>() }; ($($t:ty),*) => { { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( $(iter.next().unwrap().parse::<$t>().unwrap(),)* ) } }; } fn rec(n: usize, i: usize, visited: &mut [bool]) -> i64 { visited[i] = true; if i == n { 1 } else { let num = (0..20) .filter(|j| i & (1usize << j) != 0usize) .collect::>() .len(); let mut ret = 1 << 60; if i + num <= n && !visited[i + num] { ret = ret.min(rec(n, i + num, visited) + 1) } if 1 + num <= i && !visited[i - num] { ret = ret.min(rec(n, i - num, visited) + 1) } ret } } fn main() { let n = scan!(usize); let mut visited = vec![false; n + 1]; let n = rec(n, 1, &mut visited); if n == 1 << 60 { println!("-1") } else { println!("{}", n) } }