function main(input) {
    var n = parseInt(input, 10);

    var counter = 0;
    var a = 6;
    var b = 9;
    var tmp1 = 0;
    var tmp2 = 0;
    var tmp3 = 0;
    while(true){
    	if(n < 6) {
    		counter += n;
    		return console.log(counter);
    	}

    	for (var i = 1; i < 10000; i++) {
    		if(n - a < 0) {
    			tmp1 = a / 6;
    			break;
    		}
    		a *= 6;
    	}

    	for (var i = 1; i < 10000; i++) {
    		if(n - b < 0) {
    			tmp2 = b / 9;
    			break;
    		}
    		b *= 9;
    	}
    	//tmp1 1mp2 比較
    	tmp3 = tmp1;
    	if(tmp2 > tmp1) {
    		tmp3 = tmp2;
    	}

    	n = n - tmp3;
    	counter++;

    }

  }
  main(require('fs').readFileSync('/dev/stdin', 'utf8'));