{-# OPTIONS_GHC -O #-}
import Control.Monad
import Data.List
import Data.Maybe

d9 = [59049, 6561, 729, 81, 9, 1] :: [Int]
d6 = [46656, 7776, 1296, 216, 36, 6, 1] :: [Int]

dfs 0 c = c
dfs n c = 1 + min (dfs c6 c) (dfs c9 c)
    where 
        c9 = n - (fromMaybe undefined $ find (<=n) d9)
        c6 = n - (fromMaybe undefined $ find (<=n) d6)

main = do
    n <- readLn :: IO Int
    print $ dfs n 0