fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    var r = n
    for (i in 0..n) {
        var count = 0

        var t = i
        while (t > 0) {
            count += t % 6
            t /= 6
        }

        t = n - i
        while (t > 0) {
            count += t % 9
            t /= 9
        }

        r = Math.min(r, count)
    }
    println(r)
}