object Main {
  def main(args: Array[String]): Unit = {
    solve
  }

  def solve(): Unit = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt
    val patterns = Seq(1, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049).sorted


    // hiまでにかかるコスト
    val dp = Array.fill[Int](100001)(Int.MaxValue)
    dp(0) = 0
    for (i <- 0 until n) {
      for (pat <- patterns) {
        if (i + pat <= n) {
          dp(i + pat) = math.min(dp(i + pat), dp(i) + 1)
        }
      }
    }
    println(dp(n))
  }
}
