"use strict";
function Main(input) {
    let lines = input.split("\n");
    let N = lines[0].split(/\s/).map((v) => v - 0)[0];
    let res = N;
    for (let i = 0; i <= N; i++) {
        let cc = 0, t = i;
        while (t > 0) cc += t % 6, t /= 6, t |= 0;
        t = N - i;
        while (t > 0) cc += t % 9, t /= 9, t |= 0;
        if (res > cc) res = cc;
    }
    console.log(res);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));