fun main(args : Array<String>){
    val dp = IntArray(111111,{1000000000})
    dp[0] = 0

    for(i in 0 until 111110){
        dp[i+1] = Math.min(dp[i+1],dp[i]+1)
        var tmp = 6
        for(j in 0 until 1000) {
            if (i + tmp > 100000) break
            dp[i + tmp] = Math.min(dp[i + tmp], dp[i] + 1)
            tmp *= 6
        }
        tmp = 9
        for(j in 0 until 1000) {
            if (i + tmp > 100000) break
            dp[i + tmp] = Math.min(dp[i + tmp], dp[i] + 1)
            tmp *= 9
        }
    }

    val n = readLine()!!.toInt()
    println(dp[n])
}