import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;

T lread(T = long)()
{
    return readln.chomp.to!T();
}

T[] aryread(T = long)()
{
    return readln.split.to!(T[])();
}

void minAssign(T)(ref T dst, T src)
{
    dst = min(dst, src);
}

alias sread = () => readln.chomp();

void main()
{
    long N = lread();

    auto dp = new long[](N + 1);
    dp[] = long.max;
    dp[0] = 0;

    foreach (i; 0 .. N)
        if (dp[i] != long.max)
        {
            dp[i + 1].minAssign(dp[i] + 1);

            size_t j = 6;
            while (i + j < dp.length)
            {
                dp[i + j].minAssign(dp[i] + 1);
                j *= 6;
            }

            j = 9;
            while (i + j < dp.length)
            {
                dp[i + j].minAssign(dp[i] + 1);
                j *= 9;
            }
        }

    dp[N].writeln();
}
