import Control.Arrow
import Control.Monad
import Data.Bits
import qualified Data.ByteString.Char8 as BC
import Data.Char
import Data.List
import Data.Maybe

main :: IO ()
main = do
  n <- getInt
  print . minimum $ [sum (baseConvert 6 x) + sum (baseConvert 9 (n - x)) | x <- [0..n]]

getInt :: IO Int
getInt = readLn

baseConvert :: Int -> Int -> [Int] -- O(log n)
baseConvert b = unfoldr (\n -> if n == 0 then Nothing else Just (n `mod` b, n `div` b))
