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

sp n p b
    | n < p*b = p
    | otherwise = sp n (p*b) b

s69 n
    | n == 0 = Nothing
    | otherwise = Just ((max (sp n 1 9) (sp n 1 6)), n - (max (sp n 1 9) (sp n 1 6)))

main=do
    n<-readLn :: IO Int
    let r = takeWhile (>1) $ unfoldr s69 n
    print $ case n-(sum r) of
        0 -> length r
        1 -> length r + 1
        2 -> length r + 2
        3 -> length r + 1
        4 -> length r + 2
        5 -> length r + 3
