
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;

void main()
{
	auto n = readln.strip.to!( int );
	writeln( solve( n ) );
}

auto solve( in int n )
{
	int[] alls;
	int[] sixes = [ 6 ];
	int[] nines = [ 9 ];
	
	for( auto i = 1; sixes[ i-1 ] < n; i++ )
	{
		sixes ~= sixes[ i-1 ] * sixes[ 0 ];
	}
	
	for( auto i = 1; nines[ i-1 ] < n; i++ )
	{
		nines ~= nines[ i-1 ] * nines[ 0 ];
	}
	
	alls ~= sixes[ 0 .. $-1 ] ~ nines[ 0 .. $-1 ];
	sort( alls );
	
	return count( alls, n );
}

int count( in int[] alls, in int n )
{
	if( alls.length == 0 ) return n;

	auto c = n / alls[ $ - 1 ];
	auto r = n % alls[ $ - 1 ];
	
	if( r == 0 ) return c;
	
	switch( c )
	{
		case 0:
			return count( alls[ 0 .. $-1 ], n );
		case 1:
			auto a1 = c   + count( alls[ 0 .. $-1 ], r );
			auto a2 = c-1 + count( alls[ 0 .. $-1 ], r + alls[ $ - 1 ] );
			return min( a1, a2 );
		case 2:
			auto a1 = c   + count( alls[ 0 .. $-1 ], r );
			auto a2 = c-1 + count( alls[ 0 .. $-1 ], r + alls[ $ - 1 ] );
			auto a3 = c-2 + count( alls[ 0 .. $-1 ], r + alls[ $ - 1 ]*2 );
			return min( a1, a2, a3 );
		default:
			auto a1 = c   + count( alls[ 0 .. $-1 ], r );
			auto a2 = c-1 + count( alls[ 0 .. $-1 ], r + alls[ $ - 1 ] );
			auto a3 = c-2 + count( alls[ 0 .. $-1 ], r + alls[ $ - 1 ]*2 );
			auto a4 = c-3 + count( alls[ 0 .. $-1 ], r + alls[ $ - 1 ]*3 );
			return min( a1, a2, a3 );
	}
}

unittest
{
	assert( solve( 127 ) == 4 );
	assert( solve( 3 ) == 3 );
	assert( solve( 44852 ) == 16 );
}
