import io.StdIn.readInt

object Main extends App {
  val n = readInt

  println(Solver.solve(n))
}

object Solver {
  def solve(n: Int) = {
    (for {
      i <- 0 to n
      j = n - i
    } yield {
      val n6 = Stream.from(0).map(i => math.pow(6, i).toInt).takeWhile(_ <= i).map(pos => (math.floor(i / pos) % 6).toInt).sum
      val n9 = Stream.from(0).map(i => math.pow(9, i).toInt).takeWhile(_ <= j).map(pon => (math.floor(j / pon) % 9).toInt).sum

      n6 + n9
    }).min
  }
}