const input = require("fs").readFileSync("/dev/stdin", "utf8")

const N = parseInt(input, 10)

const dp = [...Array(N + 1).fill(null)].map((_, i) => i)

for (var i = 0; i < N; i++) {
    var j = 0
    var num = 0;
    while (i + num <= N) {
        j++
        num = Math.pow(6, j)
        if (i + num <= N) {
            dp[i + num] = Math.min(dp[i + num], dp[i] + 1)
        }
    }
    var j = 0
    var num = 0;
    while (i + num <= N) {
        j++
        num = Math.pow(9, j)
        if (i + num <= N) {
            dp[i + num] = Math.min(dp[i + num], dp[i] + 1)
        }
    }
}

console.log(dp[N])
