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 < j and pi > pj.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of swap operations applied to the sequence.

Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109, ai ≠ bi) — the arguments of the swap operation.

Output

Print a single integer — the number of inversions in the resulting sequence.

Examples

Input

2
4 2
1 4


Output

4


Input

3
1 6
3 4
2 5


Output

15

Note

In the first sample the sequence is being modified as follows: <image>. It has 4 inversions formed by index pairs (1, 4), (2, 3), (2, 4) and (3, 4).
#include <bits/stdc++.h>
using namespace std;
struct SegTree {
  struct Node {
    int l, r;
    int v1, v2;
    Node *lch, *rch;
    Node(int v1, int v2, int l, int r) {
      this->v1 = v1;
      this->v2 = v2;
      this->l = l;
      this->r = r;
      lch = rch = nullptr;
    }
  };
  int zero_v1() { return 0; }
  int zero_v2() { return 0; }
  int zero() { return 0; }
  Node *lch(Node *t) {
    if (t->lch == nullptr) {
      t->lch = new Node(zero_v1(), zero_v2(), t->l, (t->l + t->r) / 2);
    }
    return t->lch;
  }
  Node *rch(Node *t) {
    if (t->rch == nullptr) {
      t->rch = new Node(zero_v1(), zero_v2(), (t->l + t->r) / 2, t->r);
    }
    return t->rch;
  }
  void eval(Node *t) {
    lch(t)->v2 += t->v2;
    rch(t)->v2 += t->v2;
    t->v2 = 0;
  }
  int value(Node *t) { return t->v1 + t->v2 * (t->r - t->l); }
  void update(Node *t) { t->v1 = value(lch(t)) + value(rch(t)); }
  void add(Node *t, int a, int b, int v) {
    if (b <= t->l || t->r <= a) {
      return;
    } else if (a <= t->l && t->r <= b) {
      t->v2 += v;
    } else {
      eval(t);
      add(lch(t), a, b, v);
      add(rch(t), a, b, v);
      update(t);
    }
  }
  int query(Node *t, int a, int b) {
    if (b <= t->l || t->r <= a) {
      return zero();
    } else if (a <= t->l && t->r <= b) {
      return value(t);
    } else {
      eval(t);
      int r = (query(lch(t), a, b) + query(rch(t), a, b));
      update(t);
      return r;
    }
  }
  Node root;
  SegTree(int n) : root(zero_v1(), zero_v2(), 0, n) {}
  void add(int a, int b, int v) {
    if (a >= b) return;
    add(&root, a, b, v);
  }
  int query(int a, int b) {
    if (a >= b) return zero();
    return query(&root, a, b);
  }
};
int n;
int main() {
  SegTree st(1 << 30);
  scanf("%d", &n);
  map<int, int> a;
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (!a.count(x)) a[x] = x;
    if (!a.count(y)) a[y] = y;
    swap(a[x], a[y]);
  }
  int prv = 1;
  long long r = 0;
  for (auto p : a) {
    int k, v;
    tie(k, v) = p;
    r += ((prv - 1) - st.query(0, prv)) * (long long)(k - prv);
    st.add(prv, k, 1);
    r += (k - 1) - st.query(0, v);
    st.add(v, v + 1, 1);
    prv = k + 1;
  }
  cout << r << endl;
}
