import Prelude import Debug.Trace (traceShowId) import Data.Maybe (fromMaybe) main :: IO () main = do n <- read <$> getLine print $ calc n calc :: Int -> Int calc n = fromMaybe (-1) $ calc' [1] where calc' :: [Int] -> Maybe Int calc' ss@(s:ss') | s < 1 = Nothing | s > n = Nothing | elem s ss' = Nothing | s == n = Just $ length ss | otherwise = let b = countBits s in minLength (calc' ((s - b):ss)) (calc' ((s + b):ss)) minLength :: Maybe Int -> Maybe Int -> Maybe Int minLength (Just a) (Just b) = Just $ min a b minLength a@(Just _) _ = a minLength _ b@(Just _) = b minLength _ _ = Nothing type Binary = [Bool] countBits :: Int -> Int countBits n = length $ filter id $ toBinary n toBinary :: Int -> Binary toBinary n = toBinary' n $ reverse $ takeWhile (<= n) $ iterate (* 2) 1 where toBinary' :: Int -> [Int] -> Binary toBinary' i [_] = if i == 1 then [True] else [False] toBinary' i (p:ps) = if i >= p then True : toBinary' (i - p) ps else False : toBinary' i ps