fun min(a:Int,b:Int):Int{
    return if( a<b ) a
    else b
}

fun main(args: Array<String>) {

    val n = readLine()!!.toInt()
    val inf = n*100

    val dp = IntArray(n+1){ inf }
    dp[0] = 0

    for ( i in 0 until n){
        var s = 1
        var t = 1
        while( i+s <= n ){
                           dp[i+s] = min( dp[i+s], dp[i]+1 )
            if( i+t <= n ) dp[i+t] = min( dp[i+t], dp[i]+1 )
            s *= 6
            t *= 9
        }
    }

    println( dp[n] )

}

