#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
int main()
{
    int n;
    cin >> n;
    vector<P> v(n);
    vector<P> t(n);
    P one = P(0, 0);
    P two = P(0, 0);
    rep(i, n)
    {
        int x, y;
        cin >> x >> y;
        v[i] = P(x, y + 1);
    }
    sort(v.begin(),v.end());
    rep(i,n){
        t[i] = P(v[i].second - v[i].first, i);
    }
    sort(t.begin(), t.end(), greater<P>());
    vector<P> dpr(n + 1);
    vector<P> dpl(n + 1);
    dpl[0] = v.front();
    dpr[n - 1] = v.back();

    rep(i, n - 1)
    {
        dpl[i + 1].first = max(dpl[i].first, v[i + 1].first);
        dpl[i + 1].second = max(min(dpl[i].second, v[i + 1].second), dpl[i + 1].first);
        dpr[n - i - 2].first = max(dpr[n - i - 1].first, v[n - i - 2].first);
        dpr[n - i - 2].second = max(min(dpr[n - i - 1].second, v[n - i - 2].second), dpr[n - i - 2].first);
    }
    int ans = dpr[1].second - dpr[1].first + v[0].second - v[0].first;
    ans = max(ans, dpl[n - 2].second - dpl[n - 2].first + v[n - 1].second - v[n - 1].first);
    for (int i = 1; i < n - 1; i++)
    {
        P p;
        p.first = max(dpl[i - 1].first, dpr[i + 1].first);
        p.second = max(min(dpl[i - 1].second, dpr[i + 1].second), p.first);
        ans = max(ans, p.second - p.first + v[i].second - v[i].first);
    }
    rep(i,n-1){
        ans = max(ans,dpl[i].second-dpl[i].first+dpr[i+1].second - dpr[i+1].first);
    }
    for (auto i : t)
    {
        P p = v[i.second];
        if (one.first == 0)
        {
            one = p;
        }
        else if (two.first == 0)
        {
            two = p;
        }
        else
        {
            int res1, res2, res3; // 幅　one two another
            res1 = one.second - one.first + two.second - one.second;
            res1 -= max(min(one.second, p.second) - max(one.first, p.first), 0);
            res2 = one.second - one.first + two.second - one.second;
            res2 -= max(min(two.second, p.second) - max(two.first, p.first), 0);
            res3 = max(min(two.second, one.second) - max(two.first, one.first), 0);
            res3 += p.second - p.first;
            if (res1 < res2 && res2 > res3)
            {
                one.first = max(one.first, p.first);
                one.second = max(min(one.second, p.second), one.first);
            }
            else if (res1 > res2 && res1 > res3)
            {
                two.first = max(two.first, p.first);
                two.second = max(min(two.second, p.second), two.first);
            }
            else
            {
                one.first = max(one.first, two.first);
                one.second = max(min(one.second, two.second), one.first);
                two = p;
            }
        }
    }
    ans = max(one.second - one.first + two.second - two.first, ans);
    one = P(0, 0);
    two = P(0, 0);
    for (auto i : t)
    {
        P p = v[i.second];
        if (one.first == 0)
        {
            one = p;
        }
        else if (two.first == 0)
        {
            two = p;
        }
        else
        {
            int res1, res2; //減少量
            res1 = one.second - one.first;
            res1 -= max(min(one.second, p.second) - max(one.first, p.first), 0);
            res2 = two.second - two.first;
            res2 -= max(min(two.second, p.second) - max(two.first, p.first), 0);
            if (res1 < res2)
            {
                one.first = max(one.first, p.first);
                one.second = max(min(one.second, p.second), one.first);
            }
            else
            {
                two.first = max(two.first, p.first);
                two.second = max(min(two.second, p.second), two.first);
            }
        }
    }
    ans = max(ans, one.second - one.first + two.second - two.first);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3328