
tob = fromEnum . (>0)

solv n = minimum $ do
  nine <- 0 : take 5 $ iterate (*9) 9
  six <- 0 : take 6 $ iterate (*6) 6
  let am = nine + six
  if am > n
  then []
  else return $ tob nine + tob six + (n-am)

main = do
  n <- read <$> getLine
  print $ solv n