object Main extends App {
  val scan = new java.util.Scanner(System.in)
  val in = scan.next.toInt
  val ss = for (i <- (1 to 10).toList) yield math.pow(6, i).toInt
  val ns = for (i <- (1 to 10).toList) yield math.pow(9, i).toInt
  val ms = 1 :: ss ::: ns

  val memo = Array.fill(ms.length+1, 100001)(100001)
  memo(0)(0) = 0
  for ((i, n) <- (1 to ms.length).zip(ms); l <- 0 to 100000) {
    memo(i)(l) = if (l-n >= 0) math.min(memo(i-1)(l), memo(i)(l-n) + 1) else memo(i-1)(l)
  }
  println(memo(ms.length)(in))
}