fun main(args:Array<String>):Unit{
    val nine = arrayOf(1, 1, 9, 81, 729, 6561, 59049)
    val six = arrayOf(1, 6, 36, 216, 1296, 7776, 46656)
    val n = readLine()!!.toInt()
    var min = Int.MAX_VALUE
    for (i in 0 .. n){
        val res = calBy(i, nine) + calBy(n - i, six)
        if (min > res)
            min = res
    }
    println(min)
}
fun calBy(n:Int, array:Array<Int>, idx:Int = 6):Int {
    return when(n){
        0 -> 0
        else -> n / array[idx] + calBy(n % array[idx], array, idx - 1)
    }
}