fun main(args: Array<String>) {
    var N = readLine()!!.toInt()
    println(minCount(N))
}

val memo = hashMapOf<Int, Int>()
fun minCount(N: Int) : Int {
    if (N == 0) {
        return 0
    }
    val countDividableBy9 = dividableTimes(N, 9)
    val countDividableBy6 = dividableTimes(N, 6)
    val maxNinePowed = Math.pow(9.toDouble(), countDividableBy9.toDouble()).toInt()
    val maxSixPowed = Math.pow(6.toDouble(), countDividableBy6.toDouble()).toInt()

    if (memo.contains(N)) {
        return memo.get(N)!!
    }

    val result = Math.min( 1 + minCount(N - maxNinePowed), 1 + minCount(N - maxSixPowed))
    memo.set(N, result)
    return result
}

fun dividableTimes(x: Int, divider: Int) : Int {
    var tmp = x
    var count = 0
    while (tmp / divider >= 1) {
        tmp /= divider
        count++
    }

    return count
}