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 fg():
    return int(input())
def fgh():
    return [int(xx) for xx in input().split()]
def sd():
    print('YES')
def df():
    print('NO')
def s(l, r):
    return p(r) - p(l)
def p(i):
    ans = 0
    while i:
        ans += R[i]
        i -= F[i]
    return ans
def pl(i, x):
    global R
    global m
    while i <= m + 1:
        R[i] += x
        i += F[i]
F = []
for i in range(2 * 10 ** 5 + 3):
    F.append(i & (-i))
n = fg()
c = []
for i in range(n):
    a, b = fgh()
    c.append((a, b))
a = []
d = {}
for i in range(n):
    if c[i][0] not in d:
        d[c[i][0]] = len(a)
        a.append(c[i][0])
    if c[i][1] not in d:
        d[c[i][1]] = len(a)
        a.append(c[i][1])
a = sorted(a)
m = len(a)
for i in range(m):
    d[a[i]] = i
b = a[:]
for i in range(n):
    b[d[c[i][0]]], b[d[c[i][1]]] = b[d[c[i][1]]], b[d[c[i][0]]]
    d[c[i][0]], d[c[i][1]] = d[c[i][1]], d[c[i][0]]
a = sorted(a)
m = len(a)
e = {}
c = []
for i in range(m):
    e[a[i]] = i
for i in range(m):
    c.append(e[b[i]])
Q = [0] * (m + 2)
pr = [0]
Q[c[0]] = 1
ans = 0
for i in range(m + 1):
    pr.append(pr[-1] + Q[i])
R = [0] * (m + 2)
for i in range(1, m + 2):
    R[i] = pr[i] - pr[i - F[i]]
for i in range(m - 1):
    k = p(m + 1) - p(i + 1)
    ans += (a[i + 1] - a[i] - 1) * k
    pl(c[i + 1] + 1, 1)

Q = [0] * (m + 2)
pr = [0]
Q[c[-1]] = 1
for i in range(m + 1):
    pr.append(pr[-1] + Q[i])
R = [0] * (m + 2)
for i in range(1, m + 2):
    R[i] = pr[i] - pr[i - F[i]]
for i in range(m - 2, -1, -1):
    k = p(i + 1)
    ans += (a[i + 1] - a[i] - 1) * k
    pl(c[i] + 1, 1)
Q = [0] * (m + 2)
pr = [0]
Q[c[-1]] = 1
for i in range(m + 1):
    pr.append(pr[-1] + Q[i])
R = [0] * (m + 2)
for i in range(1, m + 2):
    R[i] = pr[i] - pr[i - F[i]]
for i in range(m - 2, -1, -1):
    k = p(c[i] + 1)
    ans += k
    pl(c[i] + 1, 1)
print(ans)
