#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
//typedef string S;

/*ll min(ll a, ll b) {return a < b ? a : b;}
ll min(int a, ll b) {return a < b ? a : b;}
ll min(ll a, int b) {return a < b ? a : b;}
ll min(int a, int b) {return a < b ? a : b;}

ll max(ll a, ll b) {return a > b ? a : b;}
ll max(int a, ll b) {return a > b ? a : b;}
ll max(ll a, int b) {return a > b ? a : b;}
ll max(int a, int b) {return a > b ? a : b;}*/

namespace MySpace{

};

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()

const int N = 120010;
const long long MOD = 1e9 + 7;
const int INF = 1e9;
const long long rev2 = (MOD + 1) / 2;

int n, lastl, lastr;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    multiset<long long> L, R;
    long long addL = 0, addR = 0;
    long long c = 0;
    L.insert(-INF);
    R.insert(INF);
    for (int i = 0; i < n; i++)
    {
        int l, r;
        cin >> l >> r;
        if (i != 0)
        {
            //addL -= lastr - lastl, addR += r - l;
            addL -= r - l, addR += lastr - lastl;
        }

        /*for (auto it : L)
        {
            cout << it + addL << " ";
        }
        cout << endl;
        for (auto it : R)
        {
            cout << it + addR << " ";
        }
        cout << endl << c << endl;*/

        lastl = l, lastr = r;
        auto x = L.end();
        x--;
        int Ln = *x + addL;
        auto y = R.begin();
        int R0 = *y + addR;
        if (Ln <= l && l <= R0)
        {
            L.insert(l - addL);
            R.insert(l - addR);
        } else if (l < Ln)
        {
            L.erase(x);
            R.insert(Ln - addR);
            L.insert(l - addL);
            L.insert(l - addL);
            c += Ln - l;
        } else if (R0 < l)
        {
            R.erase(y);
            L.insert(R0 - addL);
            R.insert(l - addR);
            R.insert(l - addR);
            c += l - R0;
        } else
        {
            return 1;
        }

        /*for (auto it : L)
        {
            cout << it + addL << " ";
        }
        cout << endl;
        for (auto it : R)
        {
            cout << it + addR << " ";
        }
        cout << endl << c << endl;
        cout << endl;*/
    }
    cout << c;
}


Measured memory_bytes: 9600