public static void main ( String [ ] args ) {
  Scanner scan = new Scanner ( System . in ) ;
  String S = scan . next ( ) ;
  int K = scan . nextInt ( ) ;
  int N = S . length ( ) ;
  for ( int i = 0 ;
  i < N ;
  ++ i ) for ( int k = 0 ;
  k <= K ;
  ++ k ) {
    dp [ i ] [ i + 1 ] [ k ] = 1 ;
    dp [ i ] [ i ] [ k ] = 0 ;
  }
  for ( int k = 0 ;
  k <= K ;
  ++ k ) {
    for ( int L = 2 ;
    L <= N ;
    ++ L ) {
      int i = 0 , j = L ;
      while ( j <= N ) {
        if ( k == 0 ) dp [ i ] [ j ] [ k ] = Math . max ( dp [ i + 1 ] [ j ] [ k ] , Math . max ( dp [ i ] [ j - 1 ] [ k ] , ( ( S . charAt ( i ) == S . charAt ( j - 1 ) ? dp [ i + 1 ] [ j - 1 ] [ k ] + 2 : dp [ i + 1 ] [ j - 1 ] [ k ] ) ) ) ) ;
        else dp [ i ] [ j ] [ k ] = Math . max ( dp [ i + 1 ] [ j ] [ k ] , Math . max ( dp [ i ] [ j - 1 ] [ k ] , Math . max ( dp [ i ] [ j ] [ k - 1 ] , ( ( S . charAt ( i ) == S . charAt ( j - 1 ) ? dp [ i + 1 ] [ j - 1 ] [ k ] + 2 : dp [ i + 1 ] [ j - 1 ] [ k - 1 ] + 2 ) ) ) ) ) ;
        ++ i ;
        ++ j ;
      }
    }
  }
  System . out . println ( dp [ 0 ] [ N ] [ K ] ) ;
}

public static void main ( String [ ] args ) { Scanner scan = new Scanner ( System . in ) ; String S = scan . next ( ) ; int K = scan . nextInt ( ) ; int N = S . length ( ) ; for ( int i = 0 ; i < N ; ++ i ) for ( int k = 0 ; k <= K ; ++ k ) { dp [ i ] [ i + 1 ] [ k ] = 1 ; dp [ i ] [ i ] [ k ] = 0 ; } for ( int k = 0 ; k <= K ; ++ k ) { for ( int L = 2 ; L <= N ; ++ L ) { int i = 0 , j = L ; while ( j <= N ) { if ( k == 0 ) dp [ i ] [ j ] [ k ] = Math . max ( dp [ i + 1 ] [ j ] [ k ] , Math . max ( dp [ i ] [ j - 1 ] [ k ] , ( ( S . charAt ( i ) == S . charAt ( j - 1 ) ? dp [ i + 1 ] [ j - 1 ] [ k ] + 2 : dp [ i + 1 ] [ j - 1 ] [ k ] ) ) ) ) ; else dp [ i ] [ j ] [ k ] = Math . max ( dp [ i + 1 ] [ j ] [ k ] , Math . max ( dp [ i ] [ j - 1 ] [ k ] , Math . max ( dp [ i ] [ j ] [ k - 1 ] , ( ( S . charAt ( i ) == S . charAt ( j - 1 ) ? dp [ i + 1 ] [ j - 1 ] [ k ] + 2 : dp [ i + 1 ] [ j - 1 ] [ k - 1 ] + 2 ) ) ) ) ) ; ++ i ; ++ j ; } } } System . out . println ( dp [ 0 ] [ N ] [ K ] ) ; }
(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: (type_identifier) 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) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (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: (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: (array_access array: (array_access array: (identifier) index: (identifier)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) right: (decimal_integer_literal))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (identifier)) 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 (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: (decimal_integer_literal)) declarator: (variable_declarator name: (identifier) value: (identifier))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (expression_statement (assignment_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) index: (identifier)) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) (parenthesized_expression (parenthesized_expression (ternary_expression condition: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal))))) consequence: (binary_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) right: (decimal_integer_literal)) alternative: (array_access array: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier))))))))))) alternative: (expression_statement (assignment_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) index: (identifier)) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) (parenthesized_expression (parenthesized_expression (ternary_expression condition: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal))))) consequence: (binary_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) right: (decimal_integer_literal)) alternative: (binary_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (decimal_integer_literal)))))))))))))) (expression_statement (update_expression (identifier))) (expression_statement (update_expression (identifier))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (identifier)) index: (identifier)))))))