function Main(str) {
    str=str.split(/\n/);
    var one = [1];
    var sixbox=[];
    var nightbox=[];
    for(var i = 0; i<8; i++){
        sixbox[i]=(Math.pow(6,i));
        nightbox[i]=(Math.pow(9,i+1));
    }
    var total = sixbox.concat(nightbox);
    function compareFunc(a, b) {
        return (a < b ? 1 : -1);
        // return a-b
    }
    var sortNumber = total.sort(compareFunc);
    // console.log(sortNumber);
    function sai(array,num){
    var count =0;
    for(var i = 0; i<array.length; i++){
        if(array[i]>=num){
            num = num - array[i];
            count++;
        }
    }
    if(num<6 && 1<=num){
        for(var j = 0; j<num; j++){
            count++;
        }
    }
    return count;
    }
    console.log(sai(sortNumber,str[0]));
}
 Main(require("fs").readFileSync("/dev/stdin", "utf8"));