output_description: Print the maximum possible total joyfulness of the two contests.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      L_1 R_1
      L_2 R_2
      \vdots
      L_N R_N
attempt: # -*- coding: utf-8 -*-
  """
  Created on Sat Jun 15 22:01:38 2019
  
  @author: aozora
  """
  N, *L = map(int, open(0).read().split())
  inf = []
  ls = []
  rs = []
  fl = 0
  fr = float("inf")
  for i, x in enumerate(zip(*[iter(L)] * 2)):
      l, r = x
      ls.append((l, i))
      rs.append((r, i))
      inf.append((l, r))
  ls.sort()
  rs.sort(reverse=True)
  m = 0
  x = rs[-1][0] - ls[-1][0]
  used = [False] * N
  l1 = -1
  l2 = -2
  r1 = -1
  r2 = -2
  while True:
      while l1 >= -N:
          i = ls[l1][1]
          if not used[i]:
              break
          l1 -= 1
      else:
          break
      while r1 >= -N:
          i = rs[r1][1]
          if not used[i]:
              break
          r1 -= 1
      else:
          break
      while l2 >= -N:
          i = ls[l2][1]
          if (not used[i]) and l1 > l2:
              break
          l2 -= 1
      else:
          break
      while r2 >= -N:
          i = rs[r2][1]
          if (not used[i]) and r1 > r2:
              break
          r2 -= 1
      else:
          break
      x = rs[r1][0] - ls[l1][0] + 1
      fa = m + x
  
      ll, i = ls[l1]
      lr = inf[i][1]
      a = max(ll, fl)
      b = min(lr, fr)
      lm = b - a + 1
      lx = rs[r1][0] - ls[l2][0] + 1
      la = lm + lx
  
      rr, j = rs[r1]
      rl = inf[j][0]
      p = max(rl, fl)
      q = min(rr, fr)
      rm = q - p + 1
      rx = rs[r2][0] - ls[l1][0] + 1
      ra = rm + rx
      if la > ra:
          if la > fa:
              fl = a
              fr = b
              m = lm
              used[i] = True
              l1 -= 1
          else:
              break
      else:
          if ra > fa:
              fl = p
              fr = q
              m = rm
              used[j] = True
              r1 -= 1
          else:
              break
  first = [0, float("inf")]
  second = [0, float("inf")]
  for i, flag in enumerate(used):
      if flag:
          first[0] = max(first[0], inf[i][0])
          first[1] = min(first[1], inf[i][1])
      else:
          second[0] = max(second[0], inf[i][0])
          second[1] = min(second[1], inf[i][1])
  if float("inf") in first or float("inf") in second:
      print(0)
  else:
      ans = first[1] - first[0] + 1 + second[1] - second[0] + 1
      print(max(0, ans))
  
problem_description: Statement
  
  10^9 contestants, numbered 1 to 10^9, will compete in a competition. There
  will be two contests in this competition.
  
  The organizer prepared N problems, numbered 1 to N, to use in these contests.
  When Problem i is presented in a contest, it will be solved by all contestants
  from Contestant L_i to Contestant R_i (inclusive), and will not be solved by
  any other contestants.
  
  The organizer will use these N problems in the two contests. Each problem must
  be used in exactly one of the contests, and each contest must have at least
  one problem.
  
  The _joyfulness_ of each contest is the number of contestants who will solve
  all the problems in the contest. Find the maximum possible total joyfulness of
  the two contests.
samples: [{"input": "4\n    4 7\n    1 4\n    5 8\n    2 5", "output": "6\n    \n\nThe optimal choice is:\n\n  * Use Problem 1 and 3 in the first contest. Contestant 5, 6, and 7 will solve both of them, so the joyfulness of this contest is 3.\n  * Use Problem 2 and 4 in the second contest. Contestant 2, 3, and 4 will solve both of them, so the joyfulness of this contest is 3.\n  * The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.\n\n* * *"}, {"input": "4\n    1 20\n    2 19\n    3 18\n    4 17", "output": "34\n    \n\n* * *"}, {"input": "10\n    457835016 996058008\n    456475528 529149798\n    455108441 512701454\n    455817105 523506955\n    457368248 814532746\n    455073228 459494089\n    456651538 774276744\n    457667152 974637457\n    457293701 800549465\n    456580262 636471526", "output": "540049931"}]