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

#define fi first
#define se second
typedef long long ll;

const int len = 1e5+5, inf = 1e9+5;
int l[len], r[len], sz[len];
priority_queue<ll> pq1;
priority_queue<ll, vector<ll>, greater<ll> > pq2;

int main(){
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        scanf("%d %d", &l[i], &r[i]);
        sz[i] = r[i]-l[i];
    }

    pq1.push(-inf), pq2.push(+inf);
    ll k1 = 0, k2 = 0, ans = 0, opt;
    for (int i = 1; i <= n; i++){
        k1 -= sz[i], k2 += sz[i-1];
        ll a = pq1.top() + k1, b = pq2.top() + k2;

        if (l[i] < a){
            pq1.pop();
            pq2.push(a-k2);

            pq1.push(l[i]-k1);
            pq1.push(l[i]-k1);

            opt = pq2.top() + k2;
        }
        else if (b < l[i]){
            pq2.pop();
            pq1.push(b-k1);

            pq2.push(l[i]-k2);
            pq2.push(l[i]-k2);

            opt = pq1.top() + k1;
        }
        else{
            pq1.push(l[i]-k1);

            pq2.push(l[i]-k2);

            opt = pq1.top() + k1;
        }

        ans += abs(l[i] - opt);
    }

    printf("%lld\n", ans);
    return 0;
}
