use std::io::{self, BufRead}; use std::str::FromStr; use std::collections::*; use std::cmp::*; struct Parser<'a> { tokens: &'a mut Iterator, } impl<'a> Parser<'a> { fn new(i: &'a mut Iterator) -> Self { Parser {tokens: i} } fn take(&mut self) -> T { match self.tokens.next().expect("empty iterator").parse() { Ok(x) => x, Err(_) => panic!() } } fn take_some(&mut self, n: usize) -> Vec { self.tokens.take(n).map(|s| match s.parse() { Ok(x) => x, Err(_) => panic!() } ).collect() } } fn bit_count(n: i64) -> i64 { match n { 0 => 0, _ => 1 + bit_count(n - (n&(-n))), } } fn main() { let stdin = io::stdin(); let mut tokens = stdin.lock().lines().filter_map(|x| x.ok()).flat_map(|x| x.split_whitespace().map(|s| s.to_owned()).collect::>()); let mut parser = Parser::new(&mut tokens); let n: i64 = parser.take(); let mut v = Vec::new(); v.resize((n+1) as usize, None::); let mut q = BinaryHeap::new(); q.push((-1i64,1i64)); while let Some((c, p)) = q.pop() { if p <= 0 { continue; } if p > n { continue; } if v[p as usize].is_some() { continue; } v[p as usize] = Some(c); q.push((c-1, p + bit_count(p))); q.push((c-1, p - bit_count(p))); } println!("{}", match v[n as usize] { Some(c) => -c, None => -1}); }