static int solve ( int n , int [ ] [ ] a ) {
  int oneLength = 1 ;
  for ( int [ ] ai : a ) {
    if ( ai [ 0 ] == 1 ) {
      while ( oneLength < ai . length && ai [ oneLength ] - ai [ oneLength - 1 ] == 1 ) {
        oneLength ++ ;
      }
    }
  }
  return Arrays . stream ( a ) . mapToInt ( ai -> ai . length - 1 ) . sum ( ) + ( n - 1 ) - ( oneLength - 1 ) * 2 ;
}

static int solve ( int n , int [ ] [ ] a ) { int oneLength = 1 ; for ( int [ ] ai : a ) { if ( ai [ 0 ] == 1 ) { while ( oneLength < ai . length && ai [ oneLength ] - ai [ oneLength - 1 ] == 1 ) { oneLength ++ ; } } } return Arrays . stream ( a ) . mapToInt ( ai -> ai . length - 1 ) . sum ( ) + ( n - 1 ) - ( oneLength - 1 ) * 2 ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (array_type element: (integral_type) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (enhanced_for_statement type: (array_type element: (integral_type) dimensions: (dimensions)) name: (identifier) value: (identifier) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (decimal_integer_literal)) right: (decimal_integer_literal))) consequence: (block (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))) right: (binary_expression left: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal)))) right: (decimal_integer_literal)))) body: (block (expression_statement (update_expression (identifier))))))))) (return_statement (binary_expression left: (binary_expression left: (method_invocation object: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (decimal_integer_literal))))) name: (identifier) arguments: (argument_list)) right: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal)))) right: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (decimal_integer_literal))))))