import scala.math.pow

object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt
  
  def from(b: Int, n: Int): Stream[Int] = Stream.cons(pow(b, n).toInt, from(b, n + 1))
   
  val po9 = from(9, 1).takeWhile(_ <= n).toList
  val po6  = from(6, 1).takeWhile(_ <= n).toList
  val list =  (po9 ::: po6).sorted.reverse
  
  println(solve(0, n, list))
  
  
  def solve(count: Int, yen: Int, list: List[Int]): Int = {
    if (list.isEmpty) {
      count + yen
    } else if (yen >= list.head) {
      solve(count + 1, yen - list.head, list)
    } else {
      solve(count, yen, list.tail)
    }
  }
  
}

