
tob = fromEnum . (>0)

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

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