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円引き出す
    for (j <- 0 to ((log((n-i).toDouble)/log(6.0)).toInt)) dp(i+pow(6,j).toInt) = min(dp(i+pow(6,j).toInt),dp(i)+1) //6のべき乗円引き出す
    for (j <- 0 to ((log((n-i).toDouble)/log(9.0)).toInt)) dp(i+pow(9,j).toInt) = min(dp(i+pow(9,j).toInt),dp(i)+1) //9のべき乗円引き出す
  }
  
  println(dp(n))
}