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
input = sys.stdin.readline

def query(x):
    ans = 0
    for i in range(x, 0, -i & -i):
        ans += t[i]
    return ans

def add(x):
    for i in range(x, 2*n+1, i & -i):
        t[i] += 1

n = int(input())
mp, cnt, t = {}, {}, [0]*(4*n+10)

for i in range(1, n+1):
    a, b = map(int, input().split())
    if a not in mp:
        mp[a] = a
    if b not in mp:
        mp[b] = b
    mp[a], mp[b] = mp[b], mp[a]

count = 0
for x in sorted(mp):
    count += 1
    cnt[x] = count

ans = 0
for x, y in mp.items():
    ans += abs(x-y-cnt[x]+cnt[y])
    ans += cnt[x] - query(cnt[y]) - 1
    add(cnt[y])

print(ans)

  				   						   	 	 			 	   	