fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    var ans = n
    for (six in 0..n) {
        var count = 0
        var six = six
        var nine = n - six
        while (six > 0) {
            count += six % 6
            six /= 6
        }
        while (nine > 0) {
            count += nine % 9
            nine /= 9
        }
        ans = Math.min(ans, count)
    }
    println(ans)
}