import Data.List

main :: IO()
main = do
  [n] <- map read . words <$> getLine
  print $ slv n

slv :: Integer -> Integer
slv n = minimum (zipWith (+) solveList [0..81])
  where numList = [1..8]
        newList = sortBy (flip compare) (sixList ++ nineList)
        sixList = takeWhile (<= n) [6^x | x<-numList]
        nineList = takeWhile (<= n) [9^x | x<-numList]
        solveList = map (\x -> cnt (n-x) (tail newList) 100000) [0 .. 5]

cnt :: Integer -> [Integer] -> Integer -> Integer
cnt n [] ret = ret
cnt n (l:ls) ret | n < 0 = 100000 
                 | otherwise = cnt n ls (min ret newcnt)
  where newcnt = cnt' n (l:ls) 0

cnt' :: Integer -> [Integer] -> Integer -> Integer
cnt' n [] ret = ret + n
cnt' n (s:ss) ret | n >= s = cnt' (n - s) (s:ss) (ret + 1)
                  | otherwise = cnt' n ss ret
