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;
long long int insig;
void Out(vector<long long int> x) {
  for (long long int i = 0; i < x.size(); i++) {
    cout << x[i] << " ";
  }
  cout << endl;
}
class DynamicST {
 public:
  long long int N;
  class SV {
   public:
    long long int a;
    SV() { a = 0LL; }
    SV(long long int x) { a = x; }
    SV operator&(SV X) {
      SV ANS(a + X.a);
      return ANS;
    }
  };
  class LV {
   public:
    long long int a;
    LV() { a = 0LL; }
    LV(long long int x) { a = x; }
    LV operator&(LV X) {
      LV ANS(a + X.a);
      return ANS;
    }
  };
  SV neuts;
  LV neutl;
  class node {
   public:
    long long int ind;
    SV sv;
    LV lv;
    long long int l, r;
    node *par, *lson, *rson;
    node(long long int ind2, SV sv2, LV lv2, node *par2) {
      ind = ind2;
      sv = sv2;
      lv = lv2;
      lson = nullptr;
      rson = nullptr;
      par = par2;
      if (ind == 1) {
        l = 0LL;
      } else {
        if (ind % 2 == 0) {
          par->lson = this;
          l = par->l;
          r = (par->l + par->r) / 2LL;
        } else {
          par->rson = this;
          l = (par->l + par->r + 1) / 2;
          r = par->r;
        }
      }
    }
  };
  node *root;
  SV upval(node *X) {
    SV ANS((X->sv).a + (X->r - X->l + 1) * (X->lv).a);
    return ANS;
  }
  DynamicST(long long int n) {
    N = (long long int)1 << (long long int)ceil(log2(n));
    node *X = new node(1, neuts, neutl, nullptr);
    root = X;
    root->r = N - 1LL;
  }
  void prop(node *Y) {
    Y->sv = upval(Y);
    Y->lson->lv = Y->lv & Y->lson->lv;
    Y->rson->lv = Y->lv & Y->rson->lv;
    Y->lv = neutl;
  }
  SV query(long long int a, long long int b, node *Y) {
    long long int x = Y->l;
    long long int y = Y->r;
    if (y < a || x > b) {
      return neuts;
    }
    if (x >= a && y <= b) {
      return upval(Y);
    }
    if (Y->lson == nullptr) {
      node *X = new node(2 * Y->ind, neuts, neutl, Y);
    }
    if (Y->rson == nullptr) {
      node *X = new node(2 * Y->ind + 1, neuts, neutl, Y);
    }
    prop(Y);
    SV ans = query(a, b, Y->lson) & query(a, b, Y->rson);
    return ans;
  }
  void update(LV s, long long int a, long long int b, node *Y) {
    long long int x = Y->l;
    long long int y = Y->r;
    if (y < a || x > b) {
      return;
    }
    if (x >= a && y <= b) {
      Y->lv = s & Y->lv;
      return;
    }
    if (Y->lson == nullptr) {
      node *X = new node(2 * Y->ind, neuts, neutl, Y);
    }
    if (Y->rson == nullptr) {
      node *X = new node(2 * Y->ind + 1, neuts, neutl, Y);
    }
    update(s, a, b, Y->lson);
    update(s, a, b, Y->rson);
    Y->sv = upval(Y->lson) & upval(Y->rson);
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long int N;
  cin >> N;
  map<long long int, long long int> a;
  set<long long int> pos;
  long long int pa, push_back;
  for (long long int i = 0; i < N; i++) {
    cin >> pa >> push_back;
    pa--;
    push_back--;
    pos.insert(pa);
    pos.insert(push_back);
    if (a.find(pa) == a.end()) {
      a[pa] = pa;
    }
    if (a.find(push_back) == a.end()) {
      a[push_back] = push_back;
    }
    long long int xx = a[pa];
    a[pa] = a[push_back];
    a[push_back] = xx;
  }
  vector<long long int> arr;
  for (long long int i = 0; i < 4 * N; i++) {
    arr.push_back(0LL);
  }
  DynamicST S(4 * N);
  map<long long int, long long int> in;
  set<long long int>::iterator it = pos.begin();
  for (long long int i = 0; i < pos.size(); i++) {
    in[*it] = 2 * i + 1;
    it++;
  }
  it = pos.begin();
  long long int ans = 0;
  long long int val;
  long long int re;
  long long int ind = 0;
  for (long long int i = 0; i < pos.size(); i++) {
    val = in[a[*it]];
    if (i == 0) {
      if (*it != 0) {
        S.update(*it, ind, ind, S.root);
      }
      ind++;
      ans += S.query(val + 1, S.N - 1, S.root).a;
      S.update(1, val, val, S.root);
      ind++;
    } else {
      it--;
      re = -*it;
      it++;
      re += *it;
      re--;
      ans += re * S.query(ind + 1, S.N - 1, S.root).a;
      S.update(re, ind, ind, S.root);
      ind++;
      ans += S.query(val + 1, S.N - 1, S.root).a;
      S.update(1, val, val, S.root);
      ind++;
    }
    it++;
  }
  cout << ans << endl;
  return 0;
}
