#include <algorithm>
#include <array>
#include <assert.h>
#include <chrono>
#include <bitset>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <random>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ld> vld;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<ld>> vvld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<pll> vpll;

#ifdef OLBOEB
mt19937 rnd(228 + 1488 + 238 + 24111997 % 322);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

void print_time() {
#ifdef OLBOEB
    cout.flush();
    cerr << "\ntime: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
}

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sz(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }

ll id_pool = 1e8;
const ll INF = 1e10;

struct Line {
    Line(ll l, ll r, ll id) : l(l), r(r), id(id) {}
    Line() : Line(0, 0, -1) {}
    ll l, r, id;
    ll len() { assert(r >= l); return r - l; }
    Line intersect(const Line& other) {
        Line res;
        res.l = max(l, other.l);
        res.r = min(r, other.r);
        res.id = ++id_pool;
        res.r = max(res.r, res.l);
        return res;
    }
    bool empty() { return l >= r; }
};


void upd(ll& x, ll y) { x = max(x, y); }


int main() {
    cout << fixed << setprecision(20);
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);


    ll n;
    cin >> n;
    vector<Line> lines;
    ll maxlen = 0;

    for (ll i = 0; i < n; ++i) {
        ll l, r;
        cin >> l >> r;
        ++r;
        lines.eb(l, r, i);
        maxlen = max(maxlen, lines.back().len());
        auto& curr = lines.back();
    }


    sort(all(lines), [](const Line& x, const Line& y) {
        if (x.r != y.r) return x.r < y.r;
        if (x.l != y.l) return x.l < y.l;
        if (x.id != y.id) return x.id < y.id;
    });
    Line left = lines.front();

    sort(all(lines), [](const Line& x, const Line& y) {
        if (x.l != y.l) return x.l > y.l;
        if (x.r != y.r) return x.r > y.r;
        if (x.id != y.id) return x.id > y.id;
    });

    Line right = lines.front();

    ll ans = maxlen;

    if (left.id == right.id) {
        ll second_maxlen = 0;
        for (ll i = 0; i < n; ++i) {
            if (lines[i].id == left.id) continue;
            second_maxlen = max(second_maxlen, lines[i].len());
        }
        cout << (second_maxlen + left.len()) << endl;
        return 0;
    } else {
        ll third_maxlen = 0;
        for (ll i = 0; i < n; ++i) {
            if (lines[i].id == left.id) continue;
            if (lines[i].id == right.id) continue;
            third_maxlen = max(third_maxlen, lines[i].len());
        }
        upd(ans, left.intersect(right).len() + third_maxlen);
    }


    ll left_id = left.id;
    ll right_id = right.id;

    vector<Line> big;
    for (auto& line : lines) {
        if (line.id == left_id || line.id == right_id) continue;
        Line li = left.intersect(line);
        Line ri = right.intersect(line);

        if (li.empty()) {
            right = right.intersect(line);
        } else if (ri.empty()) {
            left = left.intersect(line);
        } else {
            big.pb(line);
        }

        if (left.empty() || right.empty()) {
            cout << maxlen << endl;
            return 0;
        }
    }

    if (big.empty()) {
        cout << max(maxlen, left.len() + right.len()) << endl;
        return 0;
    }

    sort(all(big), [](const Line& x, const Line& y) {
        if (x.l != y.l) return x.l < y.l;
        if (x.r != y.r) return x.r < y.r;
        if (x.id != y.id) return x.id > y.id;
    });

    multiset<ll> right_ends;
    for (Line line : big) {
        right_ends.insert(line.r);
    }
    auto kek = [&]() {
        ll left_len = left.len();
        ll right_r = right.r;
        if (!right_ends.empty()) right_r = min(right_r, *right_ends.begin());
        ll right_len = max(0LL, (right_r - right.l));
        upd(ans, left_len + right_len);
    };

    kek();
    ll big_size = sz(big);
    for (ll i = 0; i < big_size; ++i) {
        left = left.intersect(big[i]);
        right_ends.erase(right_ends.find(big[i].r));
        kek();
    }

    cout << ans << endl;
    print_time();
    return 0;
}


Measured memory_bytes: 10096