import java.math.*

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

    for (i in 0..N) {
        var cnt = 0
        var t = i
        while (t > 0) {
            cnt += t % 6
            t /= 6
        }
        t = N - i
        while (t > 0) {
            cnt += t % 9
            t /= 9
        }
        ans = Math.min(ans, cnt)
    }

    println(ans)
}

fun readInputLine(): String {
    return readLine()!!
}
