#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int inf = 1000000007;
const long long linf = 1000000000000000000LL;
const unsigned long long ulinf = (1ULL << 63) - 10ULL;
const double eps = 0.000001;
const double pi = 3.14159265358979323846;
template <class T>
T abs(T a) {
  return a >= 0 ? a : -a;
}
template <class T>
int sign(T a) {
  return a > eps ? 1 : a < -eps ? -1 : 0;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <class T>
T mod(T a, T b) {
  T ret = a % b;
  if (ret < 0) ret += b;
  return ret;
}
template <class T>
T lowbit(T x) {
  return x & -x;
}
long long addmod(long long a, long long b, long long c) {
  return ((a + b) % c + c) % c;
}
long long mulmod(long long a, long long b, long long c) {
  if (b == 0LL) return 0LL;
  long long ret = mulmod(a, b >> 1, c);
  ret = (ret + ret) % c;
  if (b & 1LL) ret = (ret + a) % c;
  return ret;
}
long long powmod(long long a, long long b, long long c) {
  if (b == 0LL) return 1LL;
  long long ret = powmod(a, b >> 1, c);
  ret = ret * ret % c;
  if (b & 1LL) ret = ret * a % c;
  return ret;
}
long long modinv(long long a, long long b) { return powmod(a, b - 2LL, b); }
template <class T>
void maxe(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
void mine(T &a, T b) {
  if (a > b) a = b;
}
int iszero(double a) { return a < eps && a > -eps; }
template <class T>
void geti(T &a) {
  a = 0;
  int b = 0;
  char c;
  while ((c = getchar()) == ' ' || c == '\n')
    ;
  if (c == '-')
    b = 1;
  else
    a = c - 48;
  while ((c = getchar()) != ' ' && c != '\n') a = a * 10 + c - 48;
  if (b) a = -a;
}
void fileio_in_out() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}
void fileio_txt() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
const int N = 222222;
const int M = 1111111;
const int K = 20;
const long long md = inf;
int test;
int n, m, k;
long long ans;
int a[N], b[N];
map<int, int> pos;
int c[N], id[N];
struct SegTree {
  int left, right;
  int num;
} seg[N << 2];
void build(SegTree *tree, int node, int l, int r) {
  tree[node].left = l;
  tree[node].right = r;
  tree[node].num = 0;
  if (l == r) return;
  int mid = (tree[node].left + tree[node].right) >> 1;
  build(tree, (node << 1), l, mid);
  build(tree, (node << 1 | 1), mid + 1, r);
}
void update(SegTree *tree, int node, int l, int r, int k) {
  if (l == tree[node].left && r == tree[node].right) {
    tree[node].num += k;
    return;
  }
  int mid = (tree[node].left + tree[node].right) >> 1;
  if (mid >= r)
    update(tree, (node << 1), l, r, k);
  else {
    if (mid < l)
      update(tree, (node << 1 | 1), l, r, k);
    else {
      update(tree, (node << 1), l, mid, k);
      update(tree, (node << 1 | 1), mid + 1, r, k);
    }
  }
}
int query(SegTree *tree, int node, int s) {
  if (tree[node].left == tree[node].right) return tree[node].num;
  int mid = (tree[node].left + tree[node].right) >> 1;
  if (mid >= s)
    return tree[node].num + query(tree, (node << 1), s);
  else
    return tree[node].num + query(tree, (node << 1 | 1), s);
}
int main() {
  scanf("%d", &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &a[i], &b[i]);
    c[++n] = a[i];
    c[++n] = b[i];
  }
  sort(c + 1, c + n + 1);
  n = unique(c + 1, c + n + 1) - (c + 1);
  for (int i = 1; i <= n; i++) {
    pos[c[i]] = i;
    id[i] = i;
  }
  for (int i = 1; i <= k; i++) swap(id[pos[a[i]]], id[pos[b[i]]]);
  build(seg, 1, 1, n);
  for (int i = 1; i <= n; i++) {
    if (id[i] > 1) update(seg, 1, 1, id[i] - 1, 1);
    ans += query(seg, 1, id[i]);
    int x = c[id[i]] - id[i];
    int y = c[i] - i;
    ans += abs(x - y);
  }
  printf("%I64d\n", ans);
  return 0;
}
