不知哪裡有錯,送出是NA(70%)
while True:
try:
a=int(input())
x=[]
for i in range(a):
y=list(map(int,input().split()))
x.append(y)
for i in range(len(x)):
for j in range(len(x)-1):
if x[j][0]>x[j+1][0]:
x[j],x[j+1]=x[j+1],x[j]
b=x[0][1]-x[0][0]
for i in range(len(x)-1):
if (x[i+1][1]>=x[i][1])and(x[i+1][0]<=x[i][1]): #[1,3],[2,4]
b+=x[i+1][1]-x[i][1]
elif (x[i+1][1]>=x[i][1])and(x[i+1][0]>=x[i][1]): #[1,2],[3,4]
b+=x[i+1][1]-x[i+1][0]
elif (x[i+1][1]<=x[i][1])and(x[i+1][0]<=x[i][1]): #[1,4],[2,3]
b+=0
x[i+1][1]=x[i][1]
print(b)
except:
break