There is an infinite sequence consisting of all positive integers in the increasing order: *p* = {1, 2, 3, ...}. We performed *n* swap operations with this sequence. A *swap*(*a*, *b*) is an operation of swapping the elements of the sequence on positions *a* and *b*. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (*i*, *j*), that *i* &lt; *j* and *p*<sub class="lower-index">*i*</sub> &gt; *p*<sub class="lower-index">*j*</sub>.
The first line contains a single integer *n* (1 ≤ *n* ≤ 10<sup class="upper-index">5</sup>) — the number of swap operations applied to the sequence.

Each of the next *n* lines contains two integers *a*<sub class="lower-index">*i*</sub> and *b*<sub class="lower-index">*i*</sub> (1 ≤ *a*<sub class="lower-index">*i*</sub>, *b*<sub class="lower-index">*i*</sub> ≤ 10<sup class="upper-index">9</sup>, *a*<sub class="lower-index">*i*</sub> ≠ *b*<sub class="lower-index">*i*</sub>) — the arguments of the swap operation.
import sys,random,bisect
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush
import math 
from types import GeneratorType
#from functools import cache  3.9
mod = int(1e9 + 7) #998244353
inf = int(1e20)
input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())
ii = lambda :int(input())
py = lambda :print("YES")
pn = lambda :print("NO")

class BIT1:
    """单点修改,区间和查询"""

    __slots__ = "size", "bit", "tree"

    def __init__(self, n: int):
        self.size = n
        self.bit = n.bit_length()
        self.tree = dict()

    def add(self, index: int, delta: int) -> None:
        # index 必须大于0
        while index <= self.size:
            self.tree[index] = self.tree.get(index, 0) + delta
            index += index & -index

    def _query(self, index: int) -> int: 
        #外部不要调用，这个不是单点值，是前缀和，查单点值还是用queryRange
        if index > self.size:
            index = self.size
        res = 0
        while index > 0:
            res += self.tree.get(index, 0)
            index -= index & -index
        return res

    def queryRange(self, left: int, right: int) -> int:
        return self._query(right) - self._query(left - 1)


n=ii()

arr=set()

opt=[]

for _ in range(n):
    x,y=li()
    arr|={x,y}
    opt.append([x,y])

arr=sorted(arr)

brr=arr[:]

d={a:i for i,a in enumerate(arr)}


bit=BIT1(10**9+5)

cnt=BIT1(10**9+5)

for a in arr:
    cnt.add(a,1)

for x,y in opt:
    i,j=d[x],d[y]
    arr[i],arr[j]=arr[j],arr[i]

res=0

for i,a in enumerate(arr):
    res+=bit.queryRange(a+1,10**9+1)
    if brr[i]-a>1:
        s,e=a+1,brr[i]-1
        res+=(e-s+1)-cnt.queryRange(s,e)
    if a-brr[i]>1:
        s,e=brr[i]+1,a-1
        res+=(e-s+1)-cnt.queryRange(s,e)
    bit.add(a,1)
    #print(a,res)

print(res)









