import java.util.*

fun main(args: Array<String>) {
    val pw = java.io.PrintWriter(System.out)
    Problem.solve(Scanner(System.`in`), pw)
    pw.flush()
}

object Problem {
    fun solve(sc: Scanner, pw: java.io.PrintWriter) {
        val N = sc.next().toInt()
        val C = mutableListOf(1)
        var c = 6
        while (c <= N) {
            C.add(c)
            c *= 6
        }
        c = 9
        while (c <= N) {
            C.add(c)
            c *= 9
        }
        C.distinct()
        C.sort()
        C.reverse()
        //dprint(C)

        val dp = IntArray(N + 1)
        dp[0] = 0
        for (i in 1..N) {
            val can = mutableListOf<Int>()
            C.forEach {
                if (i >= it) can.add(1 + dp[i - it])
            }
            dp[i] = can.min()!!
        }

        pw.println(dp[N])
    }
}
