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

    var dp = Array(110000) {N}
    dp[0] = 0

    for (i in 1..N) {
        var pow6 = 1
        while (pow6 <= i && pow6 <= N) {
            dp[i] = Math.min(dp[i], 1 + dp[i - pow6])
            pow6 *= 6
        }

        var pow9 = 1
        while (pow9 <= i && pow9 <= N) {
            dp[i] = Math.min(dp[i], 1 + dp[i - pow9])
            pow9 *= 9
        }
    }

    println(dp[N])
}