open System let bitCount x = let mutable x = x x <- x - ((x >>> 1) &&& 0x55555555) x <- (x &&& 0x33333333) + ((x >>> 2) &&& 0x33333333) x <- (x + (x >>> 4)) &&& 0x0f0f0f0f; x <- x + (x >>> 8); x <- x + (x >>> 16); x &&& 0x3f let isLeachable n = let dis = Array.zeroCreate (n + 1) dis.[1] <- 1 let rec visit x = let bit = bitCount x let f = x - bit let b = x + bit let newCost = dis.[x] + 1 let f1 = f >= 1 && (dis.[f] = 0 || dis.[f] > newCost) let f2 = b <= n && (dis.[b] = 0 || dis.[b] > newCost) match (f1, f2) with | true, true -> dis.[f] <- newCost dis.[b] <- newCost visit f visit b | true, false -> dis.[f] <- newCost visit f | false, true -> dis.[b] <- newCost visit b | false, false -> () visit 1 dis.[n] let N = Console.ReadLine() |> int isLeachable N |> function | 0 -> -1 | x -> x |> Console.WriteLine