import Control.Applicative import Data.List import Data.Bits (popCount) solve n = (\x -> if last x == [] then -1 else 1+length x) $ unfoldr next (1,[[1]]) where next :: (Int,[[Int]]) -> Maybe ([Int],(Int,[[Int]])) next (m,h'@(h:hs)) | n `elem` h || h == [] = Nothing | otherwise = let k = nub $ h >>= flip step h' in Just (k,(m+1,k:h')) step :: Int -> [[Int]] -> [Int] step j hs = [i|i<-fmap ((j+) . (*popCount j)) [1,-1], 1<=i, i<=n, all (notElem i) hs] main = do n <- (read :: String -> Int) <$> getLine print $ solve n