const BKRT = "\n";
const SPAC = " ";
const NOSP = "";

var arr = Array.from({length:100001},(a,b)=>b);

const one = [1];
const six = [46656,7776,1296,216,36,6];
const nine = [59049,6561,729,81,9];

function Main(input){
	input = [].concat(...input.trim().split(BKRT).map(a=>a.split(SPAC)));
  const strShift = ()=>input.shift();
  const intShift = ()=>~~input.shift();
  
  var a = intShift();
  
  for(var i = 6 ; i <= a; i++){
  	var o = [];
    o.push(arr[i-1]+1);
    for(var n of nine){
    	if(i >= n){
      	o.push(arr[i-n]+1);
        break;
      }
    }
    for(var s of six){
    	if(i >= s){
      	o.push(arr[i-s]+1);
        break;
      }
    }
    arr[i] = Math.min(...o);
  }
  return arr[a];
}

// If you are interested in the algorithms I used, 
// there is no need to read down from this line.

((b,c,d)=>"undefined"!=typeof require?console.log(Main(require("fs").readFileSync("/dev/stdin","utf8"))):[b=b.split("\n").slice(1,-1).join("\n"),c=function(){try{return Main(b)}catch(f){return f.stack||f.message}}(),d=typeof c,"string"==d||(c=JSON.stringify(c)),$("<form>").append([[10,b],0,[1,d],0,[10,c]].map(f=>$(f?"<textarea>":"<br>",f&&{rows:f[0],cols:40,text:f[1]}))).appendTo("body")])(`
44852
`);