x=eval(input()) a=[] b=[] c=[] space=0 for i in range(x): y=[int(n) for n in input().split()] a.append(y) for i in range(x): b.append(a[i][0]) b.sort() for i in b: for j in range(x): if i==a[j][0]: m=j c.append(a[j]) a.pop(m) x-=1 low=c[0][0] high=c[0][1] for i in c: if i[0]>=high: space+=(high-low) low=i[0] high=i[1] elif i[0]>=low and i[1]<=high: continue else: high=i[1] space+=(high-low) print(space)