import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)

    val N = sc.nextInt()
    var res = N
    for (i <- 0 to N) {
      var cc = 0
      var t = i
      while (t > 0) {
        cc += t % 6
        t /= 6
      }
      t = N - i
      while (t > 0) {
        cc += t % 9
        t /= 9
      }

      if (res > cc) res = cc
    }

    println(res)
  }
}