import scala.io.{StdIn => in}

object Main extends App {
  val n = in.readInt
  val dp = Array.fill(n+1)(Int.MaxValue)
  dp(0) = 0

  def loop(i:Int, num:Int, cnt:Int):Unit = {
    val d = math.pow(num,cnt).toInt
    if(d <= i) {
      dp(i) = math.min(dp(i), dp(i-d)+1)
      loop(i, num, cnt+1)
    }
  }

  for(i <- 1 to n) {
    loop(i, 6, 0)
    loop(i, 9, 1)
  }

  println(dp(n))
}