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, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_sum(i):
    s = 0
    while i > 0:
        s += tree[i]
        i -= i & -i
    return s

def add(i, x):
    while i < len(tree):
        tree[i] += x
        i += i & -i

n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]
s = [0]
for a, b in ab:
    s.append(a)
    s.append(b)
s = list(set(sorted(s)))
s.sort()
d = dict()
l = len(s)
for i in range(l):
    d[s[i]] = i
x = [i for i in range(l)]
for a, b in ab:
    u, v = d[a], d[b]
    x[u], x[v] = x[v], x[u]
tree = [0] * (l + 5)
ans = 0
for i in x[::-1][:-1]:
    ans += get_sum(i)
    add(i, 1)
for i in range(1, l):
    j = x[i]
    ans += abs(s[i] - s[j])
    ans -= abs(i - j)
print(ans)