public static void main ( String [ ] args ) {
  ArrayList < Pair < Integer , Integer >> list = new ArrayList < Pair < Integer , Integer >> ( ) ;
  HashSet < Integer > a = new HashSet < Integer > ( ) ;
  HashSet < Integer > b = new HashSet < Integer > ( ) ;
  int n = in . nextInt ( ) ;
  int m = in . nextInt ( ) ;
  boolean [ ] visited = new boolean [ n + 1 ] ;
  int maxA = - 1 ;
  int minB = Integer . MAX_VALUE ;
  int tmp = m ;
  while ( tmp -- > 0 ) {
    int u = in . nextInt ( ) ;
    int v = in . nextInt ( ) ;
    int min = Math . min ( u , v ) ;
    int max = Math . max ( u , v ) ;
    maxA = Math . max ( maxA , min ) ;
    minB = Math . min ( minB , max ) ;
    list . add ( new Pair ( min , max ) ) ;
    a . add ( min ) ;
    b . add ( max ) ;
    visited [ u ] = visited [ v ] = true ;
  }
  if ( m == 0 ) {
    System . out . println ( n - 1 ) ;
  }
  else {
    boolean ans = true ;
    for ( Pair p : list ) {
      if ( ( a . contains ( p . getKey ( ) ) && a . contains ( p . getValue ( ) ) ) || ( b . contains ( p . getKey ( ) ) && b . contains ( p . getValue ( ) ) ) ) {
        ans = false ;
        break ;
      }
    }
    if ( ans && maxA < minB ) {
      int count = 0 ;
      for ( int i = maxA ;
      i <= minB ;
      i ++ ) {
        if ( ! visited [ i ] ) {
          count ++ ;
        }
      }
      System . out . println ( count + 1 ) ;
    }
    else {
      System . out . println ( "0" ) ;
    }
  }
}

public static void main ( String [ ] args ) { ArrayList < Pair < Integer , Integer > > list = new ArrayList < Pair < Integer , Integer > > ( ) ; HashSet < Integer > a = new HashSet < Integer > ( ) ; HashSet < Integer > b = new HashSet < Integer > ( ) ; int n = in . nextInt ( ) ; int m = in . nextInt ( ) ; boolean [ ] visited = new boolean [ n + 1 ] ; int maxA = - 1 ; int minB = Integer . MAX_VALUE ; int tmp = m ; while ( tmp -- > 0 ) { int u = in . nextInt ( ) ; int v = in . nextInt ( ) ; int min = Math . min ( u , v ) ; int max = Math . max ( u , v ) ; maxA = Math . max ( maxA , min ) ; minB = Math . min ( minB , max ) ; list . add ( new Pair ( min , max ) ) ; a . add ( min ) ; b . add ( max ) ; visited [ u ] = visited [ v ] = true ; } if ( m == 0 ) { System . out . println ( n - 1 ) ; } else { boolean ans = true ; for ( Pair p : list ) { if ( ( a . contains ( p . getKey ( ) ) && a . contains ( p . getValue ( ) ) ) || ( b . contains ( p . getKey ( ) ) && b . contains ( p . getValue ( ) ) ) ) { ans = false ; break ; } } if ( ans && maxA < minB ) { int count = 0 ; for ( int i = maxA ; i <= minB ; i ++ ) { if ( ! visited [ i ] ) { count ++ ; } } System . out . println ( count + 1 ) ; } else { System . out . println ( "0" ) ; } } }
(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: (generic_type (type_identifier) (type_arguments (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 (generic_type (type_identifier) (type_arguments (type_identifier) (type_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)))) (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)))) (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)))) (local_variable_declaration type: (array_type element: (boolean_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (boolean_type) dimensions: (dimensions_expr (binary_expression left: (identifier) right: (decimal_integer_literal)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (unary_expression operand: (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (while_statement condition: (parenthesized_expression (binary_expression left: (update_expression (identifier)) right: (decimal_integer_literal))) 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) 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 (identifier) (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (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) (identifier)))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (true)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))))) alternative: (block (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) value: (true))) (enhanced_for_statement type: (type_identifier) name: (identifier) value: (identifier) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))) right: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (false))) (break_statement))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (binary_expression left: (identifier) right: (identifier)))) consequence: (block (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: (identifier))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (unary_expression operand: (array_access array: (identifier) index: (identifier)))) consequence: (block (expression_statement (update_expression (identifier))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))))) alternative: (block (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (string_literal))))))))))