import Control.Monad

main :: IO ()
main = do
  n <- readLn
  print $ solve n 0

solve :: Int -> Int -> Int
solve 0 i = i
solve n i = solve (n - k) (i + 1)
  where
   k = maximum . filter (n >= ) $ candidates



candidates = a ++ b ++ c
a = [1]
b = filter (<= 10^5) (take 10 $ map (6^) [1..])
c = filter (<= 10^5) (take 10 $ map (9^) [1..])
