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

ll n, l, r, c;
ll decL = 0, incR = 0;
priority_queue<ll> pql;
priority_queue<ll, vector<ll>, greater<ll>> pqr;

int main() {
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin >> n >> l >> r;
	pql.push(l); pqr.push(l); c = 0;

	ll lastsz = 0, cursz = r-l+1, ans = 0;
	for(int i = 1; i < n; i++) {
		cin >> l >> r;
		lastsz = cursz; cursz = r-l+1;

		decL -= cursz - 1;
		incR += lastsz - 1;

		if(l <= pql.top() + decL) {
			ll x = pql.top() + decL;
			ans += x - l;
			pql.push(l - decL);
			pql.push(l - decL);
			pql.pop();
			pqr.push(x - incR);
		} else if(l >= pqr.top() + incR) {
			ll x = pqr.top() + incR;
			ans += l - x;
			pqr.push(l - incR);
			pqr.push(l - incR);
			pqr.pop();
			pql.push(x - decL);
		} else {
			pql.push(l - decL);
			pqr.push(l - incR);
		}
	}

	cout << ans << '\n';

	return 0;
}