fun main(args: Array<String>) {
    abc099c()
}

fun abc099c() {
    val n = readLine()!!.toLong()

    val sixTable = sortedSetOf<Long>()
    var six = 1L
    while (six < 100000) {
        sixTable.add(six)
        six *= 6
    }

    val nineTable = sortedSetOf<Long>()
    var nine = 1L
    while (nine < 100000) {
        nineTable.add(nine)
        nine *= 9
    }

    var answer = Long.MAX_VALUE
    for (i in 0..n) {
        var sixRemain = i
        var nineRemain = n - i
        var count = 0L

        while (sixRemain > 0) {
            count++
            sixRemain -= sixTable.findLast { it <= sixRemain } ?: error("なんで？")
        }
        while (nineRemain > 0) {
            count++
            nineRemain -= nineTable.findLast { it <= nineRemain } ?: error("なんで？")
        }

        answer = Math.min(answer, count)
    }

    println(answer)
}
