Problem:
Score : 600 points

Problem 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.

Constraints

2 \leq N \leq 10^5

1 \leq L_i \leq R_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the maximum possible total joyfulness of the two contests.

Sample Input 1

4
4 7
1 4
5 8
2 5

Sample Output 1

6

The optimal choice is:

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.

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.

The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.

Sample Input 2

4
1 20
2 19
3 18
4 17

Sample Output 2

34

Sample Input 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

Sample Output 3

540049931

Solution in LuaJIT (2.0.4) (judge verdict: Wrong Answer)

local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  -- for i = 1, #ary do self.stage[stagenum][i] = ary[i] end
  -- for i = #ary + 1, mul do self.stage[stagenum][i] = emptyvalue end
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local mma, mmi = math.max, math.min
local n = io.read("*n")
local l, r = {}, {}
local s_idx = {}
for i = 1, n do
  l[i], r[i] = io.read("*n", "*n")
  s_idx[i] = i
end
table.sort(s_idx, function(x, y)
  if l[x] == l[y] then
    return r[x] < r[y]
  else
    return l[x] < l[y]
  end
end)
local lmin_idx = s_idx[1]
table.sort(s_idx, function(x, y)
  if r[x] == r[y] then
    return l[x] < l[y]
  else
    return r[x] < r[y]
  end
end)
local rmax_idx = s_idx[n] == lmin_idx and s_idx[n - 1] or s_idx[n]
local lpoint, rpoint = r[lmin_idx] - l[lmin_idx] + 1, r[rmax_idx] - l[rmax_idx] + 1
local lp, rp = {}, {}
for i = 1, n do
  if i ~= rmax_idx and i ~= lmin_idx then
    table.insert(lp, mma(0, mmi(r[i], r[lmin_idx]) - l[i] + 1))
    table.insert(rp, mma(0, r[i] - mma(l[i], l[rmax_idx]) + 1))
  end
end
-- table.insert(lp, lpoint)
-- table.insert(rp, 0)
table.insert(lp, -1)
table.insert(rp, rpoint)
local t_idx = {}
for i = 1, n - 1 do
  t_idx[i] = i
end
table.sort(t_idx, function(x, y)
  return lp[x] < lp[y]
end)
local inf = 1000000007
local st = SegTree.new(n - 1, function(x, y) return mmi(x, y) end, inf)
local ans = 0
for i = 1, n - 1 do
  st:setValue(i, rp[t_idx[i]])
  local lv = i == n - 1 and lpoint or lp[t_idx[i + 1]]
  local ret = mma(0, lv) + st.stage[1][1]
  -- print(mma(0, lv), st.stage[1][1])
  ans = mma(ans, ret)
end
print(ans)