#include <bits/stdc++.h>
using namespace std;
struct _ {
  _() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  }
} _;
template <class A, class B>
ostream &operator<<(ostream &o, pair<A, B> t) {
  o << "(" << t.first << ", " << t.second << ")";
  return o;
}
template <class T>
T convert(string s) {
  T first;
  stringstream(s) >> first;
  return first;
}
template <class T>
string tostring(T first, int len = 0, char c = '0') {
  stringstream ss;
  ss << first;
  string r = ss.str();
  if (r.length() < len) r = string(len - r.length(), c) + r;
  return r;
}
template <class T>
void PV(T a, T b, int n = 0, int w = 0, string s = " ") {
  int c = 0;
  while (a != b) {
    cout << tostring(*a++, w, ' ');
    if (a != b && (n == 0 || ++c % n))
      cout << s;
    else
      cout << "\n";
  }
}
template <class T>
inline bool chmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool chmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
const long long linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 411111;
template <class T>
struct BIT {
  static const int Z = N;
  T a[Z + 1];
  int n;
  void init(int n = Z) {
    this->n = n;
    memset(a, 0, sizeof(T) * (n + 1));
  }
  BIT(int n = Z) { init(n); }
  void add(int first, T v) {
    for (int i = first; i <= n; i += i & -i) {
      a[i] += v;
    }
  }
  T sum(int first) {
    T res = 0;
    for (int i = first; i > 0; i -= i & -i) {
      res += a[i];
    }
    return res;
  }
  T sum(int L, int R) { return sum(R) - sum(L - 1); }
  T get(int first) { return sum(first) - sum(first - 1); }
  void set(int first, T v) { add(first, v - get(first)); }
};
BIT<long long> bt;
int n;
vector<int> v;
vector<pair<int, int> > q;
int d[N];
int w[N];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    q.push_back(make_pair(a, b));
    v.push_back(a);
    v.push_back(b);
  }
  sort((v).begin(), (v).end()),
      v.resize(unique((v).begin(), (v).end()) - v.begin());
  int m = v.size();
  for (int i = 0; i < m; i++) d[i] = i;
  for (int i = 0; i < n; i++) {
    int a = q[i].first, b = q[i].second;
    int ia = lower_bound((v).begin(), (v).end(), a) - v.begin();
    int ib = lower_bound((v).begin(), (v).end(), b) - v.begin();
    swap(d[ia], d[ib]);
  }
  vector<pair<int, int> > vp;
  if (v[0] != 1) vp.push_back(make_pair(1, v[0]));
  vp.push_back(make_pair(v[d[0]], v[d[0]]));
  for (int i = 1; i < v.size(); i++) {
    int l = v[i - 1] + 1;
    int r = v[i] - 1;
    if (l <= r) vp.push_back(make_pair(l, r));
    vp.push_back(make_pair(v[d[i]], v[d[i]]));
  }
  vector<pair<int, int> > tt = vp;
  sort((tt).begin(), (tt).end());
  long long res = 0;
  for (int i = 0; i < vp.size(); i++) {
    d[i] = lower_bound((tt).begin(), (tt).end(), vp[i]) - tt.begin() + 1;
    w[i] = vp[i].second - vp[i].first + 1;
    bt.add(d[i], w[i]);
    res += w[i] * bt.sum(d[i] + 1, bt.n);
  }
  cout << res << endl;
  return 0;
}
