/**
 * Author : rama_pang
 * Problem: 
 * Time   : O(N log N)
 * Memory : O(N)
 * 
 * Let dp(n, x) = minimum cost for the first n rectangles, and the n-th rectangle left endpoint is at x.
 * Let len[n] = r[n] - l[n] + 1
 * 
 * dp(n, x) = min{dp(n - 1, y) for (x - len[n - 1] + 1 <= y <= x + len[n] - 1)} + abs(l[n] - x)
 * 
 * Left f(n, x) be the plot function of dp(n, x), with f(n) as the whole plot
 * 
 * Alternatively, we define
 * g(n) = min{f(n - 1) for (x - (len[n - 1] - 1) <= y <= x + len[n] - 1)}
 * h(n) = abs(l[n] - x)
 * f(n) = g(n) + h(n)
 * 
 * We first see that f(n) is concave. Why? Since h(n) is concave, and g(n) applies transfromation to an already
 * concave function, when we added those two, the resulting function is still concave.
 * 
 * Assume we have found f(n - 1). How do we transition into f(n)?
 * First, we apply g(n).
 * 
 * Notice that since the function is concave, the left part is shifted to the left, and the right part is shifted
 * to the right by (len[n - 1] - 1). The exception is the minimummost point, which gets bigger.
 * 
 * Next, we apply h(n).
 * Notice that for all values with x <= l[n], their line gradient is decreased by 1, and for x >= l[n], the line
 * gradient is shifted up by 1.
 * 
 * We can query the minimum point to find the answer, which is at a point where the gradient is 0 (since the function
 * is concave).
 * 
 * We can use 2 priority queues maintaining the left side of the minimummost area and the right side of the minimummost
 * area.
 * 
**/


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n;
  cin >> n;

  priority_queue<lint, vector<lint>, less<lint>> lower;
  priority_queue<lint, vector<lint>, greater<lint>> upper;

  lint ans = 0;
  lint lazy_lower = 0;
  lint lazy_upper = 0;

  lint cur_len = 0, last_len = 0;
  for (int i = 0; i < n; i++) {
    lint l, r;
    cin >> l >> r;
    tie(last_len, cur_len) = make_pair(cur_len, r - l + 1);

    if (i == 0) {
      lower.emplace(l);
      upper.emplace(l);
      continue;
    }

    // Apply g(n)
    lazy_lower -= cur_len - 1;     // points <= minimum get shifted by len[n] - 1
    lazy_upper += last_len - 1; // points >= minimum get shifted by len[n - 1] - 1

    if (l <= lower.top() + lazy_lower) {
      lint top = lower.top() + lazy_lower;
      ans += top - l; // move segment to a minimum point, which is at top since top's gradient is now 0 (previously -1).
      // Apply h(n)
      lower.emplace(l - lazy_lower);
      lower.emplace(l - lazy_lower);
      upper.emplace(top - lazy_upper);
      lower.pop();
    } else if (l >= upper.top() + lazy_upper) {
      lint top = upper.top() + lazy_upper;
      ans += l - top; // move segment to a minimum point, which is at top since top's gradient is now 0 (previously 1).
      // Apply h(n)
      upper.emplace(l - lazy_upper);
      upper.emplace(l - lazy_upper);
      lower.emplace(top - lazy_lower);
      upper.pop();
    } else { // segment is already at an optimal place, so we just apply h(n)
      lower.emplace(l - lazy_lower);
      upper.emplace(l - lazy_upper);
    }
  }

  cout << ans << "\n";
  return 0;
}


Measured memory_bytes: 2036