use std::io::*; use std::str::FromStr; use utils::*; pub fn main() { let i = stdin(); let mut o = Vec::new(); run(i.lock(), &mut o); stdout().write_all(&o).unwrap(); } fn run(i: R, o: &mut W) { let mut i = ReadEx::from(i); let n = i.read::(); let r = solve(n); if r == usize::max_value() { writeln!(o, "-1") } else { writeln!(o, "{}", r) }.unwrap(); } fn solve(n: usize) -> usize { fn bits(mut n: usize) -> usize { let mut c = 0; while n != 0 { if n % 2 == 1 { c += 1; } n /= 2; } c } let mut buf = vec![usize::max_value(); n + 1]; let mut q = Vec::new(); q.push(1); buf[0] = 0; buf[1] = 1; while let Some(i) = q.pop() { let mut update = |i0, i1| { let next = buf[i0] + 1; if buf[i1] > next { buf[i1] = next; q.push(i1); } }; let b = bits(i); if b < i { update(i, i - b); } if i + b <= n { update(i, i + b); } } buf[n] } mod utils { use super::*; pub struct ReadEx { r: R, s: String, } impl ReadEx { pub fn from(r: R) -> Self { ReadEx { r: r, s: String::new(), } } pub fn read_line(&mut self) -> &str { self.s.clear(); self.r.read_line(&mut self.s).unwrap(); self.s.trim() } pub fn read(&mut self) -> T { self.read_line().parse().ok().unwrap() } } }