{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}

import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Maybe
import Data.Char
import qualified Data.ByteString.Char8 as B
import Text.Printf

readInt = ( readLn :: IO Int )
readInts = map ( read :: String -> Int ) . words <$> getLine

getList = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine

which a b f = if f then a else b
mp [ a, b ] = ( a, b )

main = do
	n <- readInt
	let
		six = reverse $ takeWhile ( <= n ) [ 6 ^ k | k <- [ 0 .. ] ]
		nine = reverse $ takeWhile ( <= n ) [ 9 ^ k | k <- [ 0 .. ] ]
	print $ minimum $ do
		a <- [ 0 .. n ]
		let
			b = n - a
		return $ solve six a + solve nine b

solve _ 0 = 0
solve (a:as) rest
	| a <= rest = 1 + solve (a:as) ( rest - a )
	| otherwise = solve as rest