public static void main ( String [ ] args ) {
  final int INF = Integer . MAX_VALUE ;
  Scanner scn = new Scanner ( System . in ) ;
  int N = scn . nextInt ( ) ;
  ArrayList < Integer > coins = new ArrayList < Integer > ( ) ;
  coins . add ( 1 ) ;
  for ( int i = 1 ;
  ;
  i ++ ) {
    int buf = ( int ) Math . pow ( 6 , i ) ;
    if ( buf > N ) break ;
    coins . add ( buf ) ;
  }
  for ( int i = 1 ;
  ;
  i ++ ) {
    int buf = ( int ) Math . pow ( 9 , i ) ;
    if ( buf > N ) break ;
    coins . add ( buf ) ;
  }
  int [ ] DP = new int [ N + 1 ] ;
  DP [ 0 ] = 0 ;
  for ( int i = 1 ;
  i <= N ;
  i ++ ) {
    DP [ i ] = INF ;
  }
  int length = coins . size ( ) ;
  for ( int i = 1 ;
  i <= N ;
  i ++ ) {
    for ( int j = 0 ;
    j < length ;
    j ++ ) {
      int coin = coins . get ( j ) ;
      if ( i >= coin ) {
        DP [ i ] = Math . min ( DP [ i ] , DP [ i - coin ] + 1 ) ;
      }
    }
  }
  System . out . println ( DP [ N ] ) ;
}

public static void main ( String [ ] args ) { final int INF = Integer . MAX_VALUE ; Scanner scn = new Scanner ( System . in ) ; int N = scn . nextInt ( ) ; ArrayList < Integer > coins = new ArrayList < Integer > ( ) ; coins . add ( 1 ) ; for ( int i = 1 ; ; i ++ ) { int buf = ( int ) Math . pow ( 6 , i ) ; if ( buf > N ) break ; coins . add ( buf ) ; } for ( int i = 1 ; ; i ++ ) { int buf = ( int ) Math . pow ( 9 , i ) ; if ( buf > N ) break ; coins . add ( buf ) ; } int [ ] DP = new int [ N + 1 ] ; DP [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { DP [ i ] = INF ; } int length = coins . size ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 0 ; j < length ; j ++ ) { int coin = coins . get ( j ) ; if ( i >= coin ) { DP [ i ] = Math . min ( DP [ i ] , DP [ i - coin ] + 1 ) ; } } } System . out . println ( DP [ N ] ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration (modifiers) type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier)))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments (type_identifier))) arguments: (argument_list)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (break_statement)) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (break_statement)) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (binary_expression left: (identifier) right: (decimal_integer_literal)))))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (decimal_integer_literal)) right: (decimal_integer_literal))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)) (binary_expression left: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (identifier))) right: (decimal_integer_literal)))))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)))))))