#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
int num[maxn], a[maxn], b[maxn];
pair<long long, long long> ex[maxn];
struct node {
  int l, r;
  int num;
} tree[4 * maxn];
void build(int id, int l, int r) {
  tree[id].l = l;
  tree[id].r = r;
  if (l == r) {
    tree[id].num = 0;
    return;
  }
  int mid = (l + r) >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid + 1, r);
}
int query(int id, int x, int y) {
  int l = tree[id].l;
  int r = tree[id].r;
  if (x <= l && y >= r) {
    return tree[id].num;
  }
  int ans = 0;
  int mid = (l + r) >> 1;
  if (x <= mid) ans += query(id << 1, x, y);
  if (y > mid) ans += query(id << 1 | 1, x, y);
  return ans;
}
void push_up(int id) {
  tree[id].num = tree[id << 1].num + tree[id << 1 | 1].num;
}
void update(int id, int x, int val) {
  int l = tree[id].l;
  int r = tree[id].r;
  if (l == r) {
    tree[id].num = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(id << 1, x, val);
  else
    update(id << 1 | 1, x, val);
  push_up(id);
}
int main() {
  int n, tot = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ex[i] = make_pair(u, v);
    a[tot++] = u, a[tot++] = v;
    a[tot++] = u + 1, a[tot++] = v + 1;
  }
  sort(a, a + tot);
  int len = unique(a, a + tot) - a;
  num[len - 1] = 1;
  for (int i = 0; i < len - 1; i++) {
    num[i] = a[i + 1] - a[i];
  }
  memcpy(b, a, sizeof(a));
  for (int i = 1; i <= n; i++) {
    ex[i].first = lower_bound(a, a + len, ex[i].first) - a;
    ex[i].second = lower_bound(a, a + len, ex[i].second) - a;
  }
  for (int i = 1; i <= n; i++)
    swap(a[ex[i].first], a[ex[i].second]),
        swap(num[ex[i].first], num[ex[i].second]);
  long long ans = 0;
  build(1, 1, len);
  for (int i = 0; i < len; i++) {
    int l = lower_bound(b, b + len, a[i]) - b + 1;
    ans += query(1, l, len) * 1ll * num[i];
    update(1, l, num[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
