import qualified Data.IntSet as S

main :: IO ()
main = print =<< solve <$> readLn

solve :: Int -> Int
solve n = go (S.singleton 0)
  where
    go s
      | n `S.member` s = 0
      | otherwise      = 1 + go s'
      where
        s' = S.filter (<= n) $ S.unions [ S.map (+w) s | w <- ws ]

    ws = 1 : [ 6 ^ p | p <- [1..5] ] ++ [ 9 ^ p | p <- [1..6] ]