#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <random>
#include <ctime>
using namespace std;

#define int long long
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
mt19937 mt_rand(time(NULL));
struct Treap {
  int k, p, cnt, lazy;
  long long sum;
  Treap *ch[2];
  Treap(int k) : k(k), sum(k), p(mt_rand()), cnt(1), lazy(0) {
    ch[0] = ch[1] = NULL;
  }
};
inline int count(Treap *t) { return t == NULL ? 0 : t->cnt; }
inline long long sum(Treap *t) { return t == NULL ? 0 : t->sum; }
inline void setLazy(Treap *t, int v) {
  if (t == NULL) return;
  t->lazy += v;
  t->k += v;
  t->sum += 1LL*v*t->cnt;
}
inline void push(Treap *t) {
  if (t == NULL || t->lazy == 0) return;
  setLazy(t->ch[0], t->lazy);
  setLazy(t->ch[1], t->lazy);
  t->lazy = 0;
}
inline Treap* update(Treap *t) {
  if (t == NULL) return t;
  push(t);
  t->cnt = 1 + count(t->ch[0]) + count(t->ch[1]);
  t->sum = t->k + sum(t->ch[0]) + sum(t->ch[1]);
  return t;
}
Treap *merge(Treap *l, Treap *r) {
  if (l == NULL) return r;
  if (r == NULL) return l;
  if (l->p > r->p) {
    push(l);
    l->ch[1] = merge(l->ch[1], r);
    return update(l);
  }
  else {
    push(r);
    r->ch[0] = merge(l, r->ch[0]);
    return update(r);
  }
}
pair<Treap*, Treap*> split_by_cnt(Treap *t, int k) {
  if (t == NULL) return {NULL, NULL};
  push(t);
  if (k <= count(t->ch[0])) {
    auto s = split_by_cnt(t->ch[0], k);
    t->ch[0] = s._2;
    return { s._1, update(t) };
  }
  else {
    auto s = split_by_cnt(t->ch[1], k-1-count(t->ch[0]));
    t->ch[1] = s._1;
    return { update(t), s._2 };
  }
}
pair<Treap*, Treap*> split_by_key(Treap *t, int k) {
  if (t == NULL) return {NULL, NULL};
  push(t);
  if (t->k < k) {
    auto s = split_by_key(t->ch[1], k);
    t->ch[1] = s._1;
    return { update(t), s._2 };
  }
  else {
    auto s = split_by_key(t->ch[0], k);
    t->ch[0] = s._2;
    return { s._1, update(t) };
  }
}

int find(Treap *t, int k) {
  if (t == NULL) return INF;
  push(t);
  if (k < count(t->ch[0])) return find(t->ch[0], k);
  else if (k == count(t->ch[0])) return t->k;
  else return find(t->ch[1], k-1-count(t->ch[0]));
}
Treap *insert(Treap *t, int k) {
  auto s = split_by_key(t, k);
  return merge(merge(s._1, new Treap(k)), s._2);
}
void add_key(Treap *t, int v) { setLazy(t, v); }

void print(Treap *t) {
  if (t == NULL) return;
  push(t);
  print(t->ch[0]);
  cout << t->k<<",";
  print(t->ch[1]);
}

int N;
int L[100000], R[100000];
Treap *S;
long long f(int x) {
  auto s = split_by_key(S, x);
  long long ret = 1LL*x*count(s._1)-sum(s._1) + sum(s._2)-1LL*x*count(s._2);
  S = merge(s._1, s._2);
  return ret;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N;
  rep(i, N) cin >> L[i] >> R[i];
  long long C = 0;
  S = insert(S, L[0]);
  S = insert(S, L[0]);
  rep(i, N-1) {
    int len = R[i+1]-L[i+1] + R[i]-L[i];
    int shift = R[i]-L[i];
    auto s = split_by_cnt(S, ((S->cnt-1)/2)+1);
    add_key(s._1, -len);
    C -= count(s._1)*len;
    S = merge(s._1, s._2);
    add_key(S, shift);
    S = insert(S, L[i+1]);
    S = insert(S, L[i+1]);
    int center = find(S, (S->cnt-1)/2);
  }
  int center = find(S, (S->cnt-1)/2);
  cout << (f(center)+C)/2 << "\n";
  return 0;
}


Measured memory_bytes: 14336