#include <bits/stdc++.h>
#define rep(i,n) for(int i=1;i<=n;i++)
#define fs first
#define sc second
#define MAXN 1000005
#define LIMIT 1500
#define LOGN 22
#define MOD 1000000009
#define pb push_back
#define ll long long
#define lc 2*pos
#define rc 2*pos+1
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    int n;
    ll l,r,ans=0;
    priority_queue<ll>lo;
    priority_queue<ll,vector<ll>,greater<ll>>hi;
    ll lazyLo=0,lazyHi=0;
    cin>>n;
    n--;
    cin>>l>>r;
    hi.push(l),lo.push(l);
    while(n){
        n--;
        lazyHi+=r-l;
        cin>>l>>r;
        lazyLo-=r-l;
        if(l>=hi.top()+lazyHi){
            ll shifted=hi.top()+lazyHi;
            ans+=l-shifted;
            hi.push(l-lazyHi);
            hi.push(l-lazyHi);
            hi.pop();
            lo.push(shifted-lazyLo);
        }else if(l<=lo.top()+lazyLo){
            ll shifted=lo.top()+lazyLo;
            ans+=shifted-l;
            lo.push(l-lazyLo);
            lo.push(l-lazyLo);
            lo.pop();
            hi.push(shifted-lazyHi);
        }else{
            hi.push(l-lazyHi);
            lo.push(l-lazyLo);
        }
        //cout<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}