function Main(s) {
  var n = s.split("\n")[0]*1;
  var dp = new Array(100001).fill(100001);
  dp[0] = 0, dp[1] = 1;
  var sixs = [], nines = [];
  var six = 6, nine = 9;
  while(six <= 100000) sixs.push(six), six *= 6;
  while(nine <= 100000) nines.push(nine), nine *= 9;
  sixs.push(Infinity), nines.push(Infinity);
  for (var i = 2; i < 100001; i++) {
    if (i < 6) { 
      dp[i] = dp[i-1] + 1
    } else if (i < 9) {
      dp[i] = Math.min(dp[i-1]+1, dp[i-6] + 1);
    } else {
      for (var j = 0; sixs[j] <= i; j++) {
        dp[i] = Math.min(dp[i], dp[i-1]+1, dp[i-sixs[j]]+1);
      }
      for (var j = 0; nines[j] <= i; j++) {
        dp[i] = Math.min(dp[i], dp[i-1]+1, dp[i-nines[j]]+1);
      }
    }
  }
  console.log(dp[n]);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));