//new.cpp
/*
Author : Hocky Yudhiono
Min 03 Mar 2019 03:21:18  WIB
Current Local Time : 15:21:18

getchar_unlocked > getchar > cin without sync > scanf > cin with sync
bool operator<(const MyStruct& rhs) const

On how to print Long Double to 5 decimal places :
printf("%.5Lf",ans);

On how to get random numbers :
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //For int
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); //For LL
cout << rng() << endl;
shuffle(isi.begin(),isi.end(),rng);

*/

//#include <unordered_map>
//#include <unordered_set>

//#include <random>
//#include <chrono>

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>

//using namespace __gnu_pbds;
using namespace std;

#if 0
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize(3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3")
#pragma GCC target("f16c")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC diagnostic error "-std=c++14"
#pragma GCC target ("string"...)
#pragma GCC push_options
#pragma GCC pop_options
#pragma GCC reset_options
#pragma GCC optimize ("O3")
#endif

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
// typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> pbds;
//If the time limit is strict, try not to use long double


#define fbo find_by_order
#define ook order_of_key
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define remove erase
//Remember to undefine if the problem is interactive
#define endl '\n'
#define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl

const double eps = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const LL LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const LL MOD = 1000000007;
const int dx[8] = {1,0,-1,0,1,1,-1,-1};
const int dy[8] = {0,1,0,-1,1,-1,1,-1};
const double PI = 3.141592653589793;

#ifdef _WIN32
#define getchar_unlocked getchar
#endif
#define GETCHAR getchar_unlocked
inline void fastll(LL &input_number) 
{
    input_number = 0;
    int ch = GETCHAR();
    int sign = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') sign=-1;
        ch = GETCHAR();
    }
    while(ch >= '0' && ch <= '9'){
        input_number = (input_number << 3)+(input_number << 1) + ch-'0';
        ch = GETCHAR();
    }
    input_number *= sign;
}

inline void open(string a){
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

inline void fasterios(){
    //Do not use if interactive
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
}

/*
6 1
2 10 0 2 4 3

10
=================

6 3
2 10 2 6 4 3

6
=================

4 1
1 4 1 4

4
=================

10 1
10 9 8 7 6 5 4 3 2 1

0
=================
3 0
1 1 3

2
=================
*/

LL n,h,l[100005],r[100005],len[100005];

priority_queue<LL> lower;
//Grafik yang slopenya negatif

priority_queue<LL,vector<LL>,greater<LL>> upper;
//Grafik yang slopenya positif

LL lazylow, lazyhigh;

int main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
    	cin >> l[i] >> r[i];
    	len[i] = r[i]-l[i];
    }
    //dp(n,k) = minimum cost untuk kayu ke n, apabila kepala kayu ke n berada pada posisi k
    //define len[i] = r[i]-l[i]
    // dp(n,k) = allmin[ k-len[i-1] <= K' <= k+len[i] ] dp(n-1,K') + |k-l[i]|
    // dp(n,k) (baru) pengen dari dp(n-1,k-len[i-1])
    // dp(n,k) (baru) pengen dari dp(n-1,k+len[i])
	LL ans = 0;
	upper.push(l[1]); lower.push(l[1]);
	for(int i = 2;i <= n;i++){
		lazylow -= len[i];
		//Lazylow would be negative
		// as we translate it more to the left
		lazyhigh += len[i-1];
		// Lazyup would be positive
		//Kasus 1
		// cout << lazylow << " " << lazyhigh << endl;
		if(l[i] <= lower.top()+lazylow){
			// cout << "Kasus 1 " << endl;
			//The update point is on the negative slope of our graph
			LL shifted = lower.top()+lazylow;
			ans += (shifted-l[i]);
			lower.push(l[i]-lazylow);
			lower.push(l[i]-lazylow);
			lower.pop();
			upper.push(shifted-lazyhigh);
		}
		// kasus 2
		else if(l[i] >= upper.top()+lazyhigh) {
			// cout << "Kasus 2 " << endl;
			//The update point is on the positive slope of our graph
			LL shifted = upper.top()+lazyhigh;
			ans += (l[i]-shifted);
			upper.push(l[i]-lazyhigh);
			upper.push(l[i]-lazyhigh);
			upper.pop();
			lower.push(shifted-lazylow);
		}
 
		// kasus 3
		else {
			// cout << "Kasus 3 " << endl;
			lower.push(l[i]-lazylow);
			upper.push(l[i]-lazyhigh);
		}
		// cout << "here got " << i << " " << ans << endl;
	}
	cout << ans << endl;
    return 0;
}

Measured memory_bytes: 4460