public static int func ( int m , int n , String [ ] s ) {
  boolean flag = true ;
  int min = Integer . MAX_VALUE ;
  for ( int i = 0 ;
  i < m ;
  i ++ ) {
    String target = s [ i ] + s [ i ] ;
    int sum = 0 ;
    for ( int j = 0 ;
    j < m ;
    j ++ ) {
      if ( i == j ) {
        continue ;
      }
      int index = target . lastIndexOf ( s [ j ] ) ;
      if ( index == - 1 ) {
        flag = false ;
        break ;
      }
      else {
        if ( index > 0 ) {
          sum += ( n - index ) ;
        }
      }
    }
    if ( ! flag ) {
      break ;
    }
    if ( min > sum ) {
      min = sum ;
    }
  }
  if ( ! flag ) {
    return - 1 ;
  }
  return min ;
}

public static int func ( int m , int n , String [ ] s ) { boolean flag = true ; int min = Integer . MAX_VALUE ; for ( int i = 0 ; i < m ; i ++ ) { String target = s [ i ] + s [ i ] ; int sum = 0 ; for ( int j = 0 ; j < m ; j ++ ) { if ( i == j ) { continue ; } int index = target . lastIndexOf ( s [ j ] ) ; if ( index == - 1 ) { flag = false ; break ; } else { if ( index > 0 ) { sum += ( n - index ) ; } } } if ( ! flag ) { break ; } if ( min > sum ) { min = sum ; } } if ( ! flag ) { return - 1 ; } return min ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) value: (true))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier)))) (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: (type_identifier) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (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 (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (continue_statement))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (unary_expression operand: (decimal_integer_literal)))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (false))) (break_statement)) alternative: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))))))))))) (if_statement condition: (parenthesized_expression (unary_expression operand: (identifier))) consequence: (block (break_statement))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (identifier))))))) (if_statement condition: (parenthesized_expression (unary_expression operand: (identifier))) consequence: (block (return_statement (unary_expression operand: (decimal_integer_literal))))) (return_statement (identifier))))