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;
using ll = long long;
const int N = 4e6 + 123;
const int INF = 1e5 + 100;
ll t[N];
void modify(int v, int tl, int tr, int pos, int val) {
  if (tl == tr) {
    t[v] += val;
    return;
  }
  int mid = (tl + tr) >> 1;
  if (mid >= pos)
    modify(v << 1, tl, mid, pos, val);
  else
    modify(v << 1 | 1, mid + 1, tr, pos, val);
  t[v] = t[v << 1] + t[v << 1 | 1];
}
ll get(int v, int tl, int tr, int l, int r) {
  if (l > tr || tl > r) return false;
  if (tl >= l && r >= tr) return t[v];
  int mid = (tl + tr) >> 1;
  return get(v << 1, tl, mid, l, r) + get(v << 1 | 1, mid + 1, tr, l, r);
}
ll solve(vector<pair<int, int>> v) {
  set<int> s;
  map<int, int> idx;
  int cnt = false;
  for (int i = 0; i < v.size(); i++) s.insert(v[i].first);
  for (auto it = s.begin(); it != s.end(); it++) idx[*it] = cnt++;
  for (int i = 0; i < v.size(); i++) v[i].first = idx[v[i].first];
  memset(t, 0, sizeof t);
  ll ans = false;
  for (int i = 0; i < v.size(); i++) {
    ll pos = v[i].second;
    ll res = get(1, 0, cnt - 1, v[i].first + 1, cnt - 1);
    ans += pos * res;
    modify(1, 0, cnt - 1, v[i].first, v[i].second);
  }
  return ans;
}
int n, x[INF], y[INF];
vector<int> v;
map<int, int> idx;
vector<pair<int, int>> a;
void add(vector<pair<int, int>> &b, int l, int r) {
  if (l > r) return;
  b.push_back(make_pair(l, r - l + 1));
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", x + i, y + i);
    v.push_back(x[i]);
    v.push_back(y[i]);
  }
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  for (int i = 0; i < v.size(); i++) idx[v[i]] = i;
  int last = 0;
  for (int i = 0; i < v.size(); i++) {
    a.push_back(make_pair(last + 1, v[i] - 1));
    last = v[i];
  }
  a.push_back(make_pair(last + 1, (int)1e9 + 1));
  for (int i = 0; i < n; i++) {
    int xx = idx[x[i]];
    int yy = idx[y[i]];
    swap(v[xx], v[yy]);
  }
  vector<pair<int, int>> b;
  for (int i = 0; i < v.size(); i++) {
    add(b, a[i].first, a[i].second);
    add(b, v[i], v[i]);
  }
  add(b, a.back().first, a.back().second);
  cout << solve(b) << endl;
  return false;
}
