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.singleton 1) where sugoroku' s acc | S.member n acc = Just s | next == acc = Nothing | otherwise = sugoroku' (s+1) next where next = S.union adv bak adv = S.filter (\x -> x<=n && x>0) $ S.map (\x -> x + bitn x) acc bak = S.filter (\x -> x<=n && x>0) $ S.map (\x -> x - bitn x) acc