public static void main ( String [ ] args ) {
  Scanner s = new Scanner ( System . in ) ;
  int n = s . nextInt ( ) ;
  Map < String , Integer > vitaminCostMap = new HashMap < > ( ) ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    int cost = s . nextInt ( ) ;
    char arr [ ] = s . next ( ) . toCharArray ( ) ;
    Arrays . sort ( arr ) ;
    vitaminCostMap . computeIfPresent ( new String ( arr ) , ( k , val ) -> Math . min ( val , cost ) ) ;
    vitaminCostMap . computeIfAbsent ( new String ( arr ) , ( k ) -> cost ) ;
  }
  int [ ] finalCostArr = new int [ 7 ] ;
  String [ ] vitaminArr = new String [ 7 ] ;
  int size = 0 ;
  for ( Map . Entry < String , Integer > entry : vitaminCostMap . entrySet ( ) ) {
    finalCostArr [ size ] = entry . getValue ( ) ;
    vitaminArr [ size ] = entry . getKey ( ) ;
    size ++ ;
  }
  getFinalCost ( finalCostArr , size , 0 , "" , 0 , vitaminArr ) ;
  System . out . println ( finalCost == Long . MAX_VALUE ? - 1 : finalCost ) ;
}

public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int n = s . nextInt ( ) ; Map < String , Integer > vitaminCostMap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int cost = s . nextInt ( ) ; char arr [ ] = s . next ( ) . toCharArray ( ) ; Arrays . sort ( arr ) ; vitaminCostMap . computeIfPresent ( new String ( arr ) , ( k , val ) -> Math . min ( val , cost ) ) ; vitaminCostMap . computeIfAbsent ( new String ( arr ) , ( k ) -> cost ) ; } int [ ] finalCostArr = new int [ 7 ] ; String [ ] vitaminArr = new String [ 7 ] ; int size = 0 ; for ( Map . Entry < String , Integer > entry : vitaminCostMap . entrySet ( ) ) { finalCostArr [ size ] = entry . getValue ( ) ; vitaminArr [ size ] = entry . getKey ( ) ; size ++ ; } getFinalCost ( finalCostArr , size , 0 , " " , 0 , vitaminArr ) ; System . out . println ( finalCost == Long . MAX_VALUE ? - 1 : finalCost ) ; }
(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 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) (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments)) 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 (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: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier))) (lambda_expression parameters: (inferred_parameters (identifier) (identifier)) body: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier))) (lambda_expression parameters: (inferred_parameters (identifier)) body: (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 (decimal_integer_literal))))) (local_variable_declaration type: (array_type element: (type_identifier) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (type_identifier) dimensions: (dimensions_expr (decimal_integer_literal))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (enhanced_for_statement type: (generic_type (scoped_type_identifier (type_identifier) (type_identifier)) (type_arguments (type_identifier) (type_identifier))) name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (update_expression (identifier))))) (expression_statement (method_invocation name: (identifier) arguments: (argument_list (identifier) (identifier) (decimal_integer_literal) (string_literal) (decimal_integer_literal) (identifier)))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (ternary_expression condition: (binary_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))) consequence: (unary_expression operand: (decimal_integer_literal)) alternative: (identifier)))))))