import scala.io.{StdIn => in}

object Main extends App {
  val n = in.readInt
  val pull = Array(1,6, 9, 36, 81, 216,726,1296,6561,7776,46656,59049)
  def rec(acc:Int, cnt:Int, p:Int):Int = {
    // println(acc + " " + pull(p) + " " + cnt)
    if(acc < 6) cnt + acc
    else if(acc >= pull(p)) rec(acc-pull(p), cnt+1, p)
    else rec(acc, cnt, p-1)
  }
  val ans = rec(n,0,11)
  println(ans)
}