fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val dp = IntArray(n + 1)
    for (i in 1..n) {
        var s = Int.MAX_VALUE
        var j = 1
        while (j <= i) {
            s = Math.min(s, dp[i - j] + 1)
            j *= 6
        }
        j = 1
        while (j <= i) {
            s = Math.min(s, dp[i - j] + 1)
            j *= 9
        }
        dp[i] = s
    }
    println(dp[n])
}
