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, U = T)(ref T dst, U src)
{
    dst = cast(T) min(dst, src);
}

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

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

    static immutable dp = () {
        enum Nmax = 10 ^^ 5;
        auto dp = new byte[](Nmax + 1);
        dp[] = byte.max;
        dp[0] = 0;

        foreach (i; 0 .. Nmax)
            if (dp[i] != byte.max)
            {
                dp[i + 1].minAssign(cast(ubyte) dp[i] + 1);

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

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

    dp[N].writeln();
}
