#include<bits/stdc++.h>
#define ll long long
#define ldb long double
#define fi first
#define se second
#define sza(a) (int)a.size()
#define pir pair<int,int>
#define pirll pair<ll,ll>
using namespace std;
const int maxn = 2e6;
const int inf = 1e9;
pir a[maxn];
int Max[maxn],Min[maxn],val[maxn],par[maxn],V[maxn],dq[maxn];
inline int findp(int x){
return (x == par[x]) ? x : par[x] = findp(par[x]);
}
inline void add(int u,int v){
int x = findp(u),y = findp(v);
if (x != y){
par[y] = x;
V[x] = min(V[x],V[y]);
}
}
vector<pir> tcon[maxn],tcha[maxn],tmin[maxn],tmax[maxn];
inline void prepare(int n){
int M = a[1].se,m = a[1].se;
for (int i = 1 ; i <= n ; i++){
M = max(M,a[i].se);
m = min(m,a[i].se);
Max[i] = M;
Min[i] = m;
}
M = a[n].se,m = a[n].se;
int pmax = 1,pmin = 1;
for (int i = n - 1 ; i > 1 ; i--){
M = max(M,a[i + 1].se),m = min(m,a[i + 1].se);
pmax = max(pmax,1);
pmin = max(pmin,1);
while (pmax <= i && Max[pmax] <= M) pmax++;
while (pmin <= i && Min[pmin] >= m) pmin++;
tcon[min(i,min(pmax,pmin))].push_back({1,a[i].fi + M - m});
//tcha
if (max(pmax,pmin) + 1 <= i)
tcha[i].push_back({max(pmax,pmin) + 1,a[i].fi});
if (pmax < pmin && pmax < i)
tmin[min(i,pmin)].push_back({pmax + 1,a[i].fi - m});
if (pmin < pmax && pmin < i)
tmax[min(i,pmax)].push_back({pmin + 1,a[i].fi + M});
}
}
int solve_con(int n){
int res = inf;
for (int i = 1 ; i <= n ; i++)
val[i] = -a[i].fi;
int m = 0;
for (int i = 1 ; i <= n ; i++){
par[i] = i;
V[i] = val[i];
while (m > 0 && val[dq[m]] >= val[i]){
add(dq[m],i);
m--;
}
dq[++m] = i;
for (pir x : tcon[i])
res = min(res,V[findp(x.fi)]+ x.se);
}
return res;
}
int solve_cha(int n){
int res = inf;
for (int i = 1 ; i <= n ; i++)
val[i] = Max[i - 1] - Min[i -1 ] - a[i].fi;
int m = 0;
for (int i = 1 ; i <= n ; i++){
par[i] = i;
V[i] = val[i];
while (m > 0 && val[dq[m]] >= val[i]){
add(dq[m],i);
m--;
}
dq[++m] = i;
for (pir x : tcha[i])
res = min(res,V[findp(x.fi)] + x.se);
}
return res;
}
int solve_tmin(int n){
int res = inf;
for (int i = 1 ; i <= n ; i++)
val[i] = Max[i - 1] - a[i].fi;
int m = 0;
for (int i = 1 ; i <= n ; i++){
par[i] = i;
V[i] = val[i];
while (m > 0 && val[dq[m]] >= val[i]){
add(dq[m],i);
m--;
}
dq[++m] = i;
for (pir x : tmin[i])
res = min(res,V[findp(x.fi)]+ x.se);
}
return res;
}
int solve_tmax(int n){
int res = inf;
for (int i = 1 ; i <= n ; i++)
val[i] = -Min[i - 1] - a[i].fi;
int m = 0;
for (int i = 1 ; i <= n ; i++){
par[i] = i;
V[i] = val[i];
while (m > 0 && val[dq[m]] >= val[i]){
add(dq[m],i);
m--;
}
dq[++m] = i;
for (pir x : tmax[i])
res = min(res,V[findp(x.fi)] + x.se);
}
return res;
}
int solve_prefix(int n){
int M = 0,m = inf,res = inf;
for (int i = n - 1 ; i > 0 ; i--){
M = max(M,a[i + 1].se);
m = min(m,a[i + 1].se);
res = min(res,a[i].fi - a[1].fi + M - m);
}
M = 0,m = inf;
for (int i = 2 ; i <= n ; i++){
M = max(M,a[i - 1].se);
m = min(m,a[i - 1].se);
res = min(res,a[n].fi - a[i].fi + M - m);
}
return res;
}
int solve(int n){
//TH1 : only
int res = INT_MAX;
res = min(res,solve_con(n));
res = min(res,solve_cha(n));
res = min(res,solve_tmin(n));
res = min(res,solve_tmax(n));
res = min(res,solve_prefix(n));
return res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// freopen("TDIV.inp","r",stdin);
// freopen("TDIV.out","w",stdout);
int n;
cin >> n;
for (int i = 1 ; i <= n ; i++) cin >> a[i].fi >> a[i].se;
prepare(n);
cout << solve(n) << "\n";
return 0;
}
I2luY2x1Z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