#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
int arr[N];
int sum[N];
pair<int, int> a[N];
pair<int, int> mem[N];
long long tree[4 * N];
int tlen = 1;
int lft[4 * N];
int rgh[4 * N];
void update(int first, int val, int l, int r, int& v) {
  if (v == 0) v = ++tlen;
  if (l == first && r == first) {
    tree[v] = val;
    return;
  }
  int md = (l + r) >> 1;
  int lv = lft[v];
  int rv = rgh[v];
  if (first <= md)
    update(first, val, l, md, lv);
  else
    update(first, val, md + 1, r, rv);
  tree[v] = tree[lv] + tree[rv];
  lft[v] = lv;
  rgh[v] = rv;
}
int get(int ql, int qr, int l, int r, int v) {
  if (!v) return 0;
  if (l == ql && r == qr) {
    return tree[v];
  }
  int md = (l + r) >> 1;
  int lv = lft[v];
  int rv = rgh[v];
  if (qr <= md) return get(ql, qr, l, md, lv);
  if (ql > md) return get(ql, qr, md + 1, r, rv);
  return get(ql, md, l, md, lv) + get(md + 1, qr, md + 1, r, rv);
}
int main() {
  int n;
  scanf("%d", &n);
  int al = 0;
  int mx = 0;
  for (int i = 0; i < n; i++) {
    int first, second;
    scanf("%d%d", &first, &second);
    arr[al++] = first;
    arr[al++] = second;
    mem[i] = pair<int, int>(first, second);
  }
  sort(arr, arr + al);
  mx = arr[al - 1] + 1;
  int j = 1;
  int len = 0;
  for (int i = 0; i < al; i++)
    if (j <= arr[i]) {
      if (j < arr[i]) a[len++] = pair<int, int>(j, arr[i] - 1);
      a[len++] = pair<int, int>(arr[i], arr[i]);
      j = arr[i] + 1;
    }
  for (int i = 0; i < len; i++)
    sum[i] = ((i) ? sum[i - 1] : 0) + a[i].second - a[i].first + 1;
  for (int i = 0; i < n; i++) {
    int fid = upper_bound(sum, sum + len, mem[i].first) - sum;
    int sid = upper_bound(sum, sum + len, mem[i].second) - sum;
    fid--;
    sid--;
    swap(a[fid], a[sid]);
  }
  long long ans = 0;
  for (int i = 0; i < len; i++) {
    long long gt = get(a[i].second, mx, 1, mx, 1);
    ans += gt * (a[i].second - a[i].first + 1);
    int v = 1;
    update(a[i].second, a[i].second - a[i].first + 1, 1, mx, v);
  }
  cout << ans << endl;
  {
    int n;
    cin >> n;
  }
  return 0;
}
