import Control.Applicative ((<$>)) import Data.List (unfoldr, foldl') import Data.Vector.Unboxed (Vector, (!), (//)) import qualified Data.Vector.Unboxed as V import Data.Sequence ((|>), ViewL(..), Seq) import qualified Data.Sequence as Sq main :: IO () main = solve <$> readLn >>= print solve :: Int -> Int solve n = loop ique ivec where loop :: Seq Int -> Vector Int -> Int loop que vec = case Sq.viewl que of (x :< xs) -> if x == n then vec ! n else let d = bitNum x ys = [y| y <- [x+d, x-d], y >= 1, y <= n, vec ! y == (-1)] nque = foldl' (\q y -> q |> y) xs ys nvec = vec // (zip ys (repeat ((vec ! x) + 1))) in loop nque nvec EmptyL -> vec ! n ivec = (V.replicate (n+1) (-1)) // [(1,1)] ique = Sq.empty |> 1 bitNum :: Int -> Int bitNum = sum . unfoldr f where f x | x == 0 = Nothing | otherwise = Just (x `mod` 2, x `div` 2)