
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[] list;
	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 ];
	}
	
	list ~= sixes[ 0 .. $-1 ] ~ nines[ 0 .. $-1 ];
	sort( list );
	
	return count( list, n );
}

auto count( in int[] list, in int n )
{
	static int[ int ][ int ] cache;
	
	if( list.length == 0 ) return n;
	
	auto m = list[ $-1 ];
	
	if( m !in cache || n !in cache[ m ] )
	{
		auto c = n / list[ $ - 1 ];
		auto r = n % list[ $ - 1 ];
		
		if( r == 0 )
		{
			cache[ m ][ n ] = c;
		}
		else
		{
			int a = n;
			foreach_reverse( i; max( 0, c-9 ) .. c+1 )
			{
				a = min( a, i + count( list[ 0 .. $-1 ], n - list[ $ - 1 ] * i ) );
			}
			cache[ m ][ n ] = a;
		}
	}
	
	return cache[ m ][ n ];
}

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