#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, ml, mr, ans, a[100005], b[100005];
multiset<ll> l, r;

multiset<ll>::iterator last (multiset<ll> &S) {
	auto it = S.end(); it--;
	return it;
}

int main()
{
	scanf("%lld",&n);
	for(ll i=1;i<=n;i++) {
		scanf("%lld%lld",&a[i],&b[i]);
	}
	l.insert(0);
	r.insert(0);
	ml = b[1]; mr = b[1];
	for(ll i=2;i<=n;i++) {
		ml -= b[i-1] - a[i-1];
		mr += b[i] - a[i];
		if(ml <= b[i] && b[i] <= mr) {
			ll GL = b[i] - ml, GR = mr - b[i];
			l.insert((*last(l))+GL);
			r.insert((*last(r))+GR);
			ml = b[i]; mr = b[i];
		}
		else if(b[i] < ml) {
			ll G = ml - b[i];
			ans += G;
			r.insert((*last(r))+mr-ml);
			auto it = last(l);
			ll V = (*it);
			l.erase(it);
			l.insert(V - G);
			l.insert(V - G);
			mr = ml; ml = mr - (V - (*last(l)));
		}
		else {
			ll G = b[i] - mr;
			ans += G;
			l.insert((*last(l))+mr-ml);
			auto it = last(r);
			ll V = (*it);
			r.erase(it);
			r.insert(V - G);
			r.insert(V - G);
			ml = mr; mr = ml + (V - (*last(r)));
		}
	}
	printf("%lld\n",ans);
}
