'use strict'

function Main(input) {
  let N = Number(input);
  let div6 = Math.floor(N / 6) * 6;
  let div9 = Math.floor(N / 9) * 9;
  let M = Math.max(div6, div9);
  let answer = N - M;
  
  while(M !== 0) {
    if (M >= 93312) {
      M -= 93312;
      answer += 2;
    } else if (M >= 59049) {
      M -= 59049;
      answer++;
    } else if (M >= 46656) {
      M -= 46656;
      answer++;
    } else if (M >= 7776) {
      M -= 7776;
      answer++;
    } else if (M >= 6561) {
      M -= 6561;
      answer++;
    } else if (M >= 1296) {
      M -= 1296;
      answer++;
    } else if (M >= 729) {
      M -= 729;
      answer++;
    } else if (M >= 216) {
      M -= 216;
      answer++;
    } else if (M >= 81) {
      M -= 81;
      answer++;
    } else if (M >= 36) {
      M -= 36;
      answer++;
    } else if (M >= 12) {
      M -= 12;
      answer += 2;
    } else if (M >= 9) {
      M -= 9;
      answer++;
    } else if (M >= 6) {
      M -= 6;
      answer++;
    } else {
      M--;
      answer++;
    }
  }
  
  console.log(answer);
  
}

Main(require("fs").readFileSync("/dev/stdin", "utf8").trim());