#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int N; cin >> N;
vector<ll> L(N), R(N);
for (int i = 0; i < N; i++) {
cin >> L[i] >> R[i];
}
ll cost = 0;
ll los = 0, his = 0;
priority_queue<ll, vector<ll>, less<ll>> lo;
priority_queue<ll, vector<ll>, greater<ll>> hi;
lo.push(L[0]), hi.push(L[0]);
for (int i = 1; i < N; i++) {
los -= (R[i] - L[i]);
his += (R[i-1] - L[i-1]);
ll vlo = lo.top() + los;
ll vhi = hi.top() + his;
if (vlo <= L[i] && L[i] <= vhi) {
lo.push(L[i] - los);
hi.push(L[i] - his);
} else if (vhi < L[i]) {
cost += L[i] - vhi;
hi.pop();
lo.push(vhi - los);
hi.push(L[i] - his);
hi.push(L[i] - his);
} else if (L[i] < vlo) {
cost += vlo - L[i];
lo.pop();
hi.push(vlo - his);
lo.push(L[i] - los);
lo.push(L[i] - los);
}
}
cout << cost << '\n';
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp1c2luZyBsbCA9IGxvbmcgbG9uZzsKCmludCBtYWluKCkgewoJaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCksIGNpbi50aWUoMCk7CglpbnQgTjsgY2luID4+IE47Cgl2ZWN0b3I8bGw+IEwoTiksIFIoTik7Cglmb3IgKGludCBpID0gMDsgaSA8IE47IGkrKykgewoJCWNpbiA+PiBMW2ldID4+IFJbaV07Cgl9CgoJbGwgY29zdCA9IDA7CglsbCBsb3MgPSAwLCBoaXMgPSAwOwoJcHJpb3JpdHlfcXVldWU8bGwsIHZlY3RvcjxsbD4sIGxlc3M8bGw+PiBsbzsKCXByaW9yaXR5X3F1ZXVlPGxsLCB2ZWN0b3I8bGw+LCBncmVhdGVyPGxsPj4gaGk7Cglsby5wdXNoKExbMF0pLCBoaS5wdXNoKExbMF0pOwoKCWZvciAoaW50IGkgPSAxOyBpIDwg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