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

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

bfs c used q =
    case find (==0) q of
        Just _ -> c
        otherwise -> bfs (c+1) (Data.IntSet.union used $ fromList nuse) nuse
            where
                nuse = Data.List.filter (\e -> notMember e used) nexts
                nexts = concat [[n- (fromMaybe undefined $ find (<=n) d9), n- (fromMaybe undefined $ find (<=n) d6)] | n<-q]

main = do
    n <- readLn :: IO Int
    print $ bfs 0 empty [n]
