You are given N problems and a range for each problem. Contestants will solve problems within the given range. Each problem must be used in exactly one of two contests. The joyfulness of a contest is the number of contestants solving all problems in that contest. Find the maximum possible total joyfulness of the two contests. Constraints: 2 ≤ N ≤ 10^5, 1 ≤ Li ≤ Ri ≤ 10^9. Input format: N, followed by N lines of Li and Ri. Output the maximum possible total joyfulness of the two contests.

# Buggy version:
N=int(input())
A=[]
dd=0
LL,RR=[],[]

for i in range(N):
  L,R=map(int, input().split())
  LL.append(L)
  RR.append(R)
  A.append((L,-R))
LL=sorted(LL)
RR=sorted(RR)
A=sorted(A)
dl,dr=0,10**9+1
el,er=0,10**9+1
D=[0 for i in range(N-1)]
E=[0 for i in range(N-1)]

for i in range(N-1):
  dl=max(dl,A[i][0])
  dr=min(dr,-A[i][1])
  D[i]=dr-dl+1
  el=max(el,A[-1-i][0])
  er=min(er,-A[-1-i][1])
  E[-i-1]=er-el+1
ans=dd
for i in range(len(D)):
  d=max(0,D[i])+max(0,E[i])
  ans=max(d,ans)
for L,R in A:
  l=LL[-1]
  r=RR[0]
  if LL[0]==L:
    l=LL[-2]
  if RR[0]==R:
    r=RR[1]
  d=(-R-L+1)+max(0,r-l+1)
  ans=max(ans,d)
print(ans)  

# Fixed version:
N=int(input())
A=[]
dd=0
LL,RR=[],[]

for i in range(N):
  L,R=map(int, input().split())
  LL.append(L)
  RR.append(R)
  A.append((L,-R))
LL=sorted(LL)
RR=sorted(RR)
A=sorted(A)
dl,dr=0,10**9+1
el,er=0,10**9+1
D=[0 for i in range(N-1)]
E=[0 for i in range(N-1)]

for i in range(N-1):
  dl=max(dl,A[i][0])
  dr=min(dr,-A[i][1])
  D[i]=dr-dl+1
  el=max(el,A[-1-i][0])
  er=min(er,-A[-1-i][1])
  E[-i-1]=er-el+1
ans=dd
for i in range(len(D)):
  d=max(0,D[i])+max(0,E[i])
  ans=max(d,ans)
for L,R in A:
  l=LL[-1]
  r=RR[0]
  if l==L:
    l=LL[-2]
  if r==-R:
    r=RR[1]
  d=(-R-L+1)+max(0,r-l+1)
  ans=max(ans,d)
print(ans)

# Buggy version:
import sys
input = sys.stdin.readline
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
lmx = 0
lmxi = -1
rmn = 10**18
rmni = -1
ansls = []
anstmp = 0
a.sort(reverse=True)
for i in range(n):
  if a[i][0] >= lmx:
    lmx = a[i][0]
    lmxi = i
  if rmn >= a[i][1]:
    rmn = a[i][1]
    rmni = i
for i in range(n):
  if i not in (lmxi,rmni):
    anstmp = max(anstmp,a[i][1]-a[i][0]+1)
ansls.append(anstmp+max(0,rmn-lmx+1))
rmnt = a[0][1]
rmnls = [0 for i in range(n)]
rmnls[-1] = a[-1][1]
for i in range(n-1)[::-1]:
  rmnls[i] = min(rmnls[i+1],a[i][1])
for i in range(n-1):
  if i in (lmxi,rmni):
    continue
  rmnt = min(rmnt,a[i][1])
  anstmp = 0
  anstmp += max(0,rmn-a[i+1][0]+1)
  anstmp += max(0,rmnt-lmx+1)
  ansls.append(anstmp)
print(max(ansls))

# Fixed version:
import sys
input = sys.stdin.readline
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
lmx = 0
lmxi = -1
rmn = 10**18
rmni = -1
ansls = []
anstmp = 0
a.sort(reverse=True)
for i in range(n):
  if a[i][0] >= lmx:
    lmx = a[i][0]
    lmxi = i
  if rmn >= a[i][1]:
    rmn = a[i][1]
    rmni = i
for i in range(n):
  if i not in (lmxi,rmni):
    anstmp = max(anstmp,a[i][1]-a[i][0]+1)
ansls.append(anstmp+max(0,rmn-lmx+1))
rmnt = a[0][1]
rmnls = [0 for i in range(n)]
rmnls[-1] = a[-1][1]
for i in range(n-1)[::-1]:
  rmnls[i] = min(rmnls[i+1],a[i][1])
for i in range(n-1):
  rmnt = min(rmnt,a[i][1])
  anstmp = 0
  anstmp += max(0,rmn-a[i+1][0]+1)
  anstmp += max(0,rmnt-lmx+1)
  ansls.append(anstmp)
print(max(ansls))

# Buggy version:
n = int(input())
L,R = [0]*n,[0]*n
for i in range(n):
    L[i],R[i] = map(int,input().split())
    R[i] += 1

M = L.index(max(L))
m = R.index(min(R))

d = [R[i] - L[i] for i in range(n)]
ans = max(d) + max(R[m] - L[M], 0)
if m == M:
    print(ans)
    exit()
if n == 2:
    print(d[m] + d[M])
    exit()
a,b = [0]*n,[0]*n
for i in range(n):
    a[i],b[i] = max(R[i]-L[M],0) , -max(R[m]-L[i],0)
ab = sorted(list(zip(a,b)),reverse = True)
mb = [0]*n
for i in range(n)[::-1]:
    if i == n-1:
        mb[i] = -ab[i][1]
    else:
        mb[i] = min(-ab[i][1], mb[i+1])
for i in range(n-1):
    ans = max(ans,min(a[i],d[M]) + min(mb[i + 1],d[m]))
ans = max(ans , d[M] + min(mb[0], d[m]), min(a[len(a)-1],d[M]) + d[m])

print(ans)


# Fixed version:
n = int(input())
L,R = [0]*n,[0]*n
for i in range(n):
    L[i],R[i] = map(int,input().split())
    R[i] += 1

M = L.index(max(L))
m = R.index(min(R))

d = [R[i] - L[i] if M != i != m else 0 for i in range(n)]
ans = max(d) + max(R[m] - L[M], 0)
d[m] = R[m] - L[m]
d[M] = R[M] - L[M]
if m == M:
    print(ans)
    exit()
if n == 2:
    print(d[m] + d[M])
    exit()
a,b = [0]*(n-2),[0]*(n-2)
j = 0
for i in range(n):
    if m != i != M:
        a[j],b[j] = max(R[i]-L[M],0) , max(R[m]-L[i],0)
        j += 1
ab = sorted(list(zip(a,b)),reverse = True)
mb = [0]*len(a)
for i in range(len(a))[::-1]:
    if i == len(a)-1:
        mb[i] = ab[i][1]
    else:
        mb[i] = min(ab[i][1], mb[i+1])
for i in range(len(a) - 1):
    ans = max(ans,min(ab[i][0],d[M]) + min(mb[i + 1],d[m]))
ans = max(ans , d[M] + min(mb[0], d[m]), min(ab[len(a)-1][0],d[M]) + d[m])

print(ans)

# Buggy version:
import sys
sys.setrecursionlimit(10 ** 9)

n = int(input())
LR = [tuple(map(int, input().split())) for _ in range(n)]

def quicksort(x):
    if x==[]: return []

    smallerSorted = quicksort([a for a in x[1:] if (a[0] < x[0][0] or (a[0] == x[0][0] and a[1] >= x[0][1])])
    biggerSorted = quicksort([a for a in x[1:] if (a[0] > x[0][0] or (a[0] == x[0][0] and a[1] < x[0][1]))])

    return(smallerSorted+[x[0]]+biggerSorted)

LR = quicksort(LR)

ans = [0] * (n+1)
temp = 10 ** 9
i = 1
for L, R in LR:
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] = temp - L + 1
    i += 1
#print(ans)
temp = 10 ** 9
i = n - 1
L = LR[-1][0]
for x, R in reversed(LR):
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] += temp - L + 1
    i -= 1

print(max(ans))

# Fixed version:
import sys
sys.setrecursionlimit(10 ** 9)

n = int(input())
LR = [tuple(map(int, input().split())) for _ in range(n)]

def quicksort(x):
    if x==[]: return []

    smallerSorted = quicksort([a for a in x[1:] if a[0] < x[0][0] or (a[0] == x[0][0] and a[1] >= x[0][1])])
    biggerSorted = quicksort([a for a in x[1:] if a[0] > x[0][0] or (a[0] == x[0][0] and a[1] < x[0][1])])

    return(smallerSorted+[x[0]]+biggerSorted)

LR = quicksort(LR)

e = [[] for _ in range(n+2)]

ans = [0] * (n+1)
temp = 10 ** 9
i = 1
for L, R in LR:
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] = temp - L + 1
    i += 1

temp = 10 ** 9
i = n - 1
L = LR[-1][0]
for x, R in reversed(LR):
    temp = min(temp, R)
    if L > temp:
        break
    ans[i] += temp - L + 1
    i -= 1

res = max(ans)
for L, R in LR:
    res = max(res, R - L + 1 + ans[0])

#print(LR)
#print(ans)
print(res)

# Buggy version:
N = int(input())
LR = [list(map(int,input().split())) for _ in range(N)]


ML = max(LR)[0]
mR = min(LR,key=lambda x:x[1])[1]

ans = max(ML-mR+1,0)+max([LR[i][1]-LR[i][0]+1 for i in range(N)])
LR.sort(key=lambda x:[x[1],x[0]])

L_ = [0]*N
for i in range(N):
    L_[i] = min(L[i],L_[i-1])

for i in range(1,N):
    ans = max(ans,max(LR[i]-ML+1,0)+max(mR-L_[i-1]+1,0))

print(ans)

# Fixed version:
N = int(input())
LR = [list(map(int,input().split())) for _ in range(N)]

LR.sort(key=lambda x:[-x[0],x[1]])
ML = LR[0][0]
MR = LR[0][1]
LR.sort(key=lambda x:[x[1],-x[0]])
mR = LR[0][1]
mL = LR[0][0]

ans = max(mR-ML+1,0)
for i in range(N):
    if LR[i] not in [[ML,MR],[mL,mR]]:
        ans = max(ans,max(mR-ML+1,0)+max(LR[i][1]-LR[i][0]+1,0))

LR.sort(key=lambda x:[x[1],x[0]])

L_ = [0]*N
for i in range(N):
    L_[i] = max(LR[i][0],L_[i-1])

for i in range(1,N):
    ans = max(ans,max(LR[i][1]-ML+1,0)+max(mR-L_[i-1]+1,0))

print(ans)

# Buggy version:
import sys
from itertools import accumulate


def calc(tasks):
    tasks_l, tasks_r = zip(*tasks)
    tasks_r_acc_fwd = list(accumulate(tasks_r, func=min))
    tasks_r_acc_bwd = list(reversed(list(accumulate(reversed(tasks_r), func=min))))
    # print(tasks)
    # print(tasks_r_acc_fwd)
    # print(tasks_r_acc_bwd)

    right_max_l = tasks_l[-1]
    score = 0
    for i in range(1, n):
        left_max_l = tasks_l[i - 1]
        left_min_r = tasks_r_acc_fwd[i - 1]
        right_min_r = tasks_r_acc_bwd[i]
        tmp_score = max(0, left_min_r - left_max_l + 1) + max(0, right_min_r - right_max_l + 1)
        score = max(score, tmp_score)
    return score


n = int(input())
appeared_lr = set()
tasks = []
for line in sys.stdin:
    l, r = list(map(int, line.split()))
    appeared_lr.add(l)
    appeared_lr.add(r)
    tasks.append((l, r))

tasks.sort()
ans1 = calc(tasks)

tasks.sort(key=lambda lr: (lr[0], -lr[1]))
ans2 = calc(tasks)

tasks.sort(key=lambda lr: (lr[1] - lr[0], lr[0], lr[1]))
ans3 = calc(tasks)

print(max(ans1, ans2, ans3))


# Fixed version:
import sys
from itertools import accumulate


def calc(tasks):
    tasks_l, tasks_r = zip(*tasks)
    tasks_l_acc_fwd = list(accumulate(tasks_l, func=max))
    tasks_l_acc_bwd = list(reversed(list(accumulate(reversed(tasks_l), func=max))))
    tasks_r_acc_fwd = list(accumulate(tasks_r, func=min))
    tasks_r_acc_bwd = list(reversed(list(accumulate(reversed(tasks_r), func=min))))
    # print(tasks)
    # print(tasks_r_acc_fwd)
    # print(tasks_r_acc_bwd)

    score = 0
    for i in range(1, n):
        left_max_l = tasks_l_acc_fwd[i - 1]
        left_min_r = tasks_r_acc_fwd[i - 1]
        right_max_l = tasks_l_acc_bwd[i]
        right_min_r = tasks_r_acc_bwd[i]
        tmp_score = max(0, left_min_r - left_max_l + 1) + max(0, right_min_r - right_max_l + 1)
        score = max(score, tmp_score)
    return score


n = int(input())
appeared_lr = set()
tasks = []
for line in sys.stdin:
    l, r = list(map(int, line.split()))
    appeared_lr.add(l)
    appeared_lr.add(r)
    tasks.append((l, r))

tasks.sort()
ans1 = calc(tasks)

tasks.sort(key=lambda lr: (lr[0], -lr[1]))
ans2 = calc(tasks)

tasks.sort(key=lambda lr: (lr[1] - lr[0], lr[0], lr[1]))
ans3 = calc(tasks)

print(max(ans1, ans2, ans3))