object Main extends App {
  import scala.math._
  
  val n = readInt
  val dp = Array.fill[Int](n+1)(Int.MaxValue)
  dp(0) = 0
  
  for (i <- 0 until n) {
    dp(i+1) = min(dp(i+1),dp(i)+1) //1円引き出す
    
    var j = 1
    var p = pow(6,j).toInt
    while(i+p <= n) {//6のべき乗円引き出す
      dp(i+p) = min(dp(i+p),dp(i)+1)
      j += 1
      p = pow(6,j).toInt
    }
    
    j = 1
    p = pow(9,j).toInt
    while(i+p <= n) {//9のべき乗円引き出す
      dp(i+p) = min(dp(i+p),dp(i)+1)
      j += 1
      p = pow(9,j).toInt
    }
  }
  
  println(dp(n))
}