import Data.List import Data.Tuple import qualified Data.Set as S bitn n = sum $ take w $ unfoldr (return . swap . flip divMod 2) n where w = ceiling (logBase 2 (fromIntegral n)) + 1 main = readLn >>= putStrLn . maybe "-1" show . sugoroku sugoroku n = sugoroku' 1 S.empty (S.singleton 1) where sugoroku' s acc next | S.null next = Nothing | S.member n next = Just s | otherwise = sugoroku' (s+1) (S.union acc next) (S.difference next' acc) where next' = S.difference (S.union adv bak) acc adv = S.filter (\x -> x<=n && x>0) $ S.map (\x -> x + bitn x) next bak = S.filter (\x -> x<=n && x>0) $ S.map (\x -> x - bitn x) next