pragma(inline, true)
void chmin(T)(ref T a, T b) {
    if (a > b) a = b;
}

int[] init() {
    enum INF = 1 << 28;
    int[] dp = new int[100010];
    foreach (i; 1..100001) {
        dp[i] = INF;
        int p6 = 1, p9 = 1;
        while (p6 <= i || p9 <= i) {
            if (i - p6 >= 0)
                chmin(dp[i], dp[i - p6] + 1);
            if (i - p9 >= 0)
                chmin(dp[i], dp[i - p9] + 1);
            p6 *= 6; p9 *= 9;
        }
    }
    return dp;
}

void main() {
    import std.conv : to;
    import std.stdio : readln, writeln;
    import std.string : chomp;
    int[] dp = init();
    writeln(dp[readln.chomp.to!int]);
}