unittest
{
	assert( [ "127" ].solve == 4 );
	assert( [ "3" ].solve == 3 );
	assert( [ "44852" ].solve == 16 );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;

void main()
{
	stdin.byLineCopy.solve.writeln;
}

auto solve( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = input.front.to!( long );
	
	// dp[ 引き出した金額 ] = 操作回数
	auto dp = new long[ 1 + n ];
	dp[ 1 .. $ ] = long.max;
	foreach( i; 0L .. dp.length.signed - 1 )
	{
		dp[ i + 1 ] = min( dp[ i + 1 ], dp[ i ] + 1 );
		for( auto j = 6L; i + j < dp.length; j *= 6 )
		{
			dp[ i + j ] = min( dp[ i + j ], dp[ i ] + 1 );
		}
		for( auto j = 9L; i + j < dp.length; j *= 9 )
		{
			dp[ i + j ] = min( dp[ i + j ], dp[ i ] + 1 );
		}
	}
	return dp[ $ - 1 ];
}
