fun main(arr:Array<String>) {
    val num = readLine()!!.toInt()
    dic.addAll((0..num).map { null })
    val ans = getAns(num)
    println(ans)
}

val dic = mutableListOf<Int?>()

fun getAns(remain:Int): Int {
    dic[remain]?.let { return it }
    if(remain == 0) {
        return 0
    }
    var ans = remain
    for(i in (1..6).reversed()) {
        val num = Math.pow(9.toDouble(), i.toDouble()).toInt()
        if(num  > remain) {
            continue
        }
        val ret = getAns(remain - num) + 1
        ans = Math.min(ret, ans)
    }
    for(i in (1..7).reversed()) {
        val num = Math.pow(6.toDouble(), i.toDouble()).toInt()
        if(num  > remain) {
            continue
        }
        val ret = getAns(remain - num) + 1
        ans = Math.min(ret, ans)
    }
    dic[remain] = ans
    return ans
}
