import Data.Bits import Control.Monad.State main = do n <- readLn print . (\x -> case x of [] -> -1 _ -> minimum x) $ evalState (bitDice 1 n) (1, []) bitDice :: Int -> Int -> State (Int, [Int]) [Int] bitDice k n = do (c, visited) <- get if k `elem` visited then return [] else if k == n then return [c] else if k > n || k < 0 then return [] else do let new_k = filter (\x -> x > 0 && x <= n) [k + popCount k, k - popCount k] put (c + 1, k : visited) return . concat . map (\s -> evalState s (c + 1, k : visited)) $ map (\s -> bitDice s n) new_k