import qualified Data.Vector.Unboxed as VU

solve :: VU.Vector Int
solve = VU.constructN (10^5+100) $ branch
    where
        branch vec
            | VU.length vec == 0 = 0
            | otherwise = value vec

        value vec = (minimum $ as ++ bs) + 1
            where
                l = VU.length vec
                six = filter (<=l) $! map (6^) [0..6]
                nine = filter (<=l) $! map (9^) [0..5]
                as = map (\i -> vec VU.! (l - i)) six
                bs  = map (\j -> vec VU.! (l - j)) nine

main = do
    n <- readLn :: IO Int
    print $ solve VU.! n