import Data.Bits import Control.Monad.State import qualified Data.Vector.Unboxed as U import Debug.Trace main = do n <- readLn print . (\x -> case x of [] -> -1 _ -> minimum x) $ evalState (bitDice 1 n) (1, U.fromList []) bitDice :: Int -> Int -> State (Int, U.Vector Int) [Int] bitDice k n = do (c, visited) <- get if k `U.elem` visited || k > n || k < 1 then return [] else if k == n then return [c] else do let nexts = [k + popCount k, k - popCount k] new_s = (c + 1, k `U.cons` visited) put new_s let (left, ls) = runState (bitDice (nexts !! 0) n) new_s put ls let (right, rs) = runState (bitDice (nexts !! 1) n) (c + 1, k `U.cons` snd ls) put rs return $ concat [left, right]