fun main(args: Array<String>) {
    var N= nextInt()
    val dp = IntArray(N + 1) { 1000000007 }
    dp[0] = 0
    for (n in 1 .. N) {
        var power = 1
        while (power <= n) {
            dp[n] = Math.min(dp[n], dp[n - power] + 1)
            power *= 6
        }
    }
    for (n in 1 .. N) {
        var power = 1
        while (power <= n) {
            dp[n] = Math.min(dp[n], dp[n - power] + 1)
            power *= 9
        }
    }
    println(dp[N])
}
fun next() = readLine()!!
fun nextInt() = next().toInt()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { Integer.parseInt(it) }
fun listOfLong() = listOfString().map { java.lang.Long.parseLong(it) }
