#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAXN = 200100;

int N;
ll L[MAXN], R[MAXN];

priority_queue<ll> lq;
priority_queue<ll,vector<ll>,greater<ll>> rq;

int main()
{
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> L[i] >> R[i];

    ll ans = 0;
    ll lq_shift = 0, rq_shift = 0;

    lq.push(L[0]);
    rq.push(L[0]);

    for (int i = 1; i < N; i++)
    {
        rq_shift += R[i-1] - L[i-1];
        lq_shift -= R[i] - L[i];

        ll ltop = lq.top() + lq_shift;
        ll rtop = rq.top() + rq_shift;
        if (L[i] < ltop)
        {
            ans += abs (ltop - L[i]);
            lq.pop();
            rq.push(ltop-rq_shift);
            lq.push(L[i]-lq_shift);
            lq.push(L[i]-lq_shift);
        }
        else if (L[i] > rtop)
        {
            ans += abs (rtop - L[i]);
            rq.pop();
            lq.push(rtop-lq_shift);
            rq.push(L[i]-rq_shift);
            rq.push(L[i]-rq_shift);
        }
        else
        {
            lq.push(L[i]-lq_shift);
            rq.push(L[i]-rq_shift);
        }
    }
    cout << ans << "\n";
}