#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
#define F first
#define S second

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

template<typename T>
static T randint(T lo, T hi) { return uniform_int_distribution<T>(lo, hi)(rng); }


struct treap
{
	struct node
	{
		node *ch[2];
		ll len, slen;
		int p, maxp, prio, sz, lazy;

		node(int l, int p) : len(l), slen(l), p(p), maxp(p)
		{
			ch[0] = ch[1] = NULL;
			lazy = 0;
			sz = 1;
			prio = randint(0, (int)2e9);
		}
	} *root;

	node* new_node(int l, int p)
	{
		return new node(l, p);
	}

	int size(node *u)
	{
		return u ? u->sz : 0;
	}

	int max_p(node *u)
	{
		return u ? (u->maxp + u->lazy) : (-2e9);
	}

	ll s_len(node *u)
	{
		return u ? u->slen : 0;
	}

	void push(node *u)
	{
		if (u && u->lazy != 0)
		{
			for (int i = 0; i < 2; ++i)
				if (u->ch[i])
					u->ch[i]->lazy += u->lazy;

			u->p += u->lazy;
			u->maxp += u->lazy;
			u->lazy = 0;
		}
	}

	node* update(node *u)
	{
		if (u)
		{
			u->sz = size(u->ch[0]) + size(u->ch[1]) + 1;
			u->maxp = max({ max_p(u->ch[0]), max_p(u->ch[1]), u->p });
			u->slen = s_len(u->ch[0]) + s_len(u->ch[1]) + u->len;
		}
		return u;
	}

	pair<node*, node*> split(node* u, int k)
	{// split for the kth first elements
		push(u);

		if (!u)
			return { u, u };

		if (size(u->ch[0]) >= k)
		{
			auto s = split(u->ch[0], k);
			u->ch[0] = s.second;
			return { s.first, update(u) };
		}

		auto s = split(u->ch[1], k - size(u->ch[0]) - 1);
		u->ch[1] = s.first;
		return { update(u), s.second };
	}

	node* merge(node *u, node *v)
	{
		push(u), push(v);

		if (!u || !v)
			return u ? u : v;

		if (u->prio > v->prio)
		{
			u->ch[1] = merge(u->ch[1], v);
			return update(u);
		}

		v->ch[0] = merge(u, v->ch[0]);
		return update(v);
	}

	pair<node*, node*> split1(node* u)
	{
		push(u);

		if (!u)
			return { u, u };

		if (max(max_p(u->ch[0]), u->p) >= 0)
		{
			auto s = split1(u->ch[0]);
			u->ch[0] = s.second;
			return { s.first, update(u) };
		}

		auto s = split1(u->ch[1]);
		u->ch[1] = s.first;
		return { update(u), s.second };
	}

	pair<node*, node*> split2(node* u, ll k)
	{
		push(u);

		if (!u)
			return { u, u };

		if (s_len(u->ch[0]) + u->len >= k)
		{
			auto s = split2(u->ch[0], k);
			u->ch[0] = s.second;
			return { s.first, update(u) };
		}

		auto s = split2(u->ch[1], k - s_len(u->ch[0]) - u->len);
		u->ch[1] = s.first;
		return { update(u), s.second };
	}

	void dfs(node *u, ll &x, ll &y)
	{
		if (!u) return;

		push(u);

		dfs(u->ch[0], x, y);

		if (u->p < 0)
		{
			x += u->len;
			y += u->len * u->p;
		}

		dfs(u->ch[1], x, y);
	}

	treap() : root(NULL) {}
};

struct info
{
	ll l, o;
};

int main()
{
	#ifdef DGC
		freopen("a.txt", "r", stdin);
		//freopen("b.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int n;
	cin >> n;
	vector<info> a(n);
	for (auto &i : a)
	{
		cin >> i.o >> i.l;
		i.l -= i.o;
	}

	ll x = 0, y = a[0].o;

	treap t;
	t.root = t.merge(t.root, t.new_node(a[0].o, -1));
	t.root = t.merge(t.root, t.new_node(1e9+5, +1));

	for (int i = 1; i < n; ++i)
	{
		auto s = t.split1(t.root);
		t.root = t.merge(t.merge(s.first, t.new_node(a[i].l + a[i-1].l, 0)), s.second);

		x -= a[i].l;
		y += a[i].o - x;

		s = t.split2(t.root, a[i].o - x + 1);
		auto s2 = t.split(s.second, 1);
		ll l = t.s_len(s.first);
		if (l < a[i].o - x) s.first = t.merge(s.first, t.new_node(a[i].o - x - l, t.max_p(s2.first)));
		if (l + s2.first->len > a[i].o - x) s2.second = t.merge(t.new_node(l + s2.first->len - (a[i].o - x), t.max_p(s2.first)), s2.second);
		--s.first->lazy;
		++s2.second->lazy;
		t.root = t.merge(s.first, s2.second);
	}

	t.dfs(t.root, x, y);
	cout << y << "\n";

	return 0;
}


Measured memory_bytes: 20608