import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;

string my_readln() { return chomp(readln()); }

long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }

void main()
{
	auto tokens = my_readln.split;
	auto N = tokens[0].to!long;

	ulong[] six, nine;

	uint x = 6;
	while (x <= N)
	{
		six ~= x;
		x *= 6;
	}
	uint y = 9;
	while (y <= N)
	{
		nine ~= y;
		y *= 9;
	}

	auto dp = new ulong[](N+1);
	fill(dp, ulong.max);
	dp[0] = 0;
	foreach (i; 0..N)
	{
		if (dp[i] == ulong.max) continue;

		foreach (e; six)
		{
			auto j = i + e;
			if (j <= N)
			{
				dp[j] = min(dp[j], dp[i] + 1);
			}
		}
		foreach (e; nine)
		{
			auto j = i + e;
			if (j <= N)
			{
				dp[j] = min(dp[j], dp[i] + 1);
			}
		}
		dp[N] = min(dp[N], dp[i] + N - i);
	}

	writeln(dp[N]);
	stdout.flush();
}