// http://yukicoder.me/problems/11 open System let dprintfn fmt = Printf.kprintf Diagnostics.Debug.WriteLine fmt let bitcount n = let rec loop bits num = if bits = 0 then num else loop (bits &&& (bits-1)) (num+1) loop n 0 let solve N = let dp = Array.init (N+1) (fun _ -> -1) let rec f i ans = dp.[i] <- if dp.[i] > 0 then min dp.[i] ans else ans if i = N then () else let n = bitcount i if i+n < N+1 && dp.[i+n] = -1 then f (i+n) (ans+1) if i-n > 0 && dp.[i-n] = -1 then f (i-n) (ans+1) f 1 1 dp.[N] [] let main argv = let N = Console.ReadLine() |> int solve N |> printfn "%d" 0