public static void main ( String [ ] args ) throws Exception {
  Scanner sc = new Scanner ( System . in ) ;
  int n = sc . nextInt ( ) ;
  int x = sc . nextInt ( ) ;
  int [ ] [ ] s = new int [ n ] [ 2 ] ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    int a = sc . nextInt ( ) ;
    int b = sc . nextInt ( ) ;
    if ( a < b ) {
      s [ i ] [ 0 ] = a ;
      s [ i ] [ 1 ] = b ;
    }
    else {
      s [ i ] [ 0 ] = b ;
      s [ i ] [ 1 ] = a ;
    }
  }
  int maxStart = Integer . MIN_VALUE ;
  int minEnd = Integer . MAX_VALUE ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    if ( s [ i ] [ 0 ] > maxStart ) maxStart = s [ i ] [ 0 ] ;
    if ( s [ i ] [ 1 ] < minEnd ) minEnd = s [ i ] [ 1 ] ;
  }
  if ( maxStart > minEnd ) System . out . println ( - 1 ) ;
  else if ( maxStart < x && x < minEnd ) System . out . println ( 0 ) ;
  else if ( x <= maxStart ) {
    System . out . println ( maxStart - x ) ;
  }
  else {
    System . out . println ( x - minEnd ) ;
  }
}

public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int x = sc . nextInt ( ) ; int [ ] [ ] s = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; if ( a < b ) { s [ i ] [ 0 ] = a ; s [ i ] [ 1 ] = b ; } else { s [ i ] [ 0 ] = b ; s [ i ] [ 1 ] = a ; } } int maxStart = Integer . MIN_VALUE ; int minEnd = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] [ 0 ] > maxStart ) maxStart = s [ i ] [ 0 ] ; if ( s [ i ] [ 1 ] < minEnd ) minEnd = s [ i ] [ 1 ] ; } if ( maxStart > minEnd ) System . out . println ( - 1 ) ; else if ( maxStart < x && x < minEnd ) System . out . println ( 0 ) ; else if ( x <= maxStart ) { System . out . println ( maxStart - x ) ; } else { System . out . println ( x - minEnd ) ; } }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) (throws (type_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: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (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 (identifier)) dimensions: (dimensions_expr (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 (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)))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal)) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal)) right: (identifier)))) alternative: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal)) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal)) right: (identifier))))))) (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: (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 (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal)) right: (identifier))) consequence: (expression_statement (assignment_expression left: (identifier) right: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal))))) (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal)) right: (identifier))) consequence: (expression_statement (assignment_expression left: (identifier) right: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (unary_expression operand: (decimal_integer_literal))))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (identifier) right: (identifier)))) consequence: (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (decimal_integer_literal)))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (identifier)))))) alternative: (block (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (identifier)))))))))))