#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <list>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
#ifdef DEBUG
template<typename T>
class Vec : public vector<T>{
public:
    Vec(){}
    Vec(int n):vector<T>(n){}
    Vec(int n, T a):vector<T>(n,a){}
    T& operator[](long long n){
        if(n < 0 or n >= this->size())throw out_of_range("Vec");
        return this->at(n);
    }
    const T& operator[](long long n)const{
        if(n < 0 or n >= this->size())throw out_of_range("Vec");
        return this->at(n);
    }
};
#define vector Vec
template<typename T>
ostream &operator<<(ostream &os, set<T> st) {
    for(auto x : st) cerr << x << ' ';
    return os;
}
template<typename T, typename S>
ostream &operator<<(ostream &os, map<T, S> mp) {
    for(auto x : mp) cerr << x << ' ';
    return os;
}
#endif
int naive(vector<pii>a){
    int n = (int) a.size();
    int res = 0;
    vector<int> s;
    for(int i = 0; i < 1 << n; i++){
        set<int>st;
        for(int j = 0; j < n; j++){
            if(i >> j & 1)st.insert(j);
        }
        if(st.size() == n or st.size() == 0){
            s.push_back(-1);
            continue;
        }
        int l = -1;
        int r = 1e9+1;
        for(int x : st){
            l = max(l, a[x].first);
            r = min(r, a[x].second);
        }
        int temp = max(0, r - l + 1);
        l = -1;
        r = 1e9+1;
        rep(x,n){
            if(st.count(x))continue;
            l = max(l, a[x].first);
            r = min(r, a[x].second);
        }
        temp += max(0, r - l + 1);
        s.push_back(temp);
    }
    show(s);
    int id = (int)(max_element(s.begin(), s.end()) - s.begin());
    show(id, s[id]);
    show(bitset<10>(id));
    return s[id];
}
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<pii> a;
    srand(time(nullptr));
    rep(i,n){
        int l, r;
        cin >> l >> r;
//        l = rand() % 1000000000;
//        r = rand() % 1000000000;
        a.push_back(minmax(l, r));
    }
    sort(a.begin(), a.end(), [&](pii x, pii y){
        if(x.second == y.second){
            return x.first < y.first;
        }else{
            return x.second < y.second;
        }
    });
    show(a);
//        show(naive(a));
    vector<int> dp(n, -1);
    {
        int l = -1, r = 1e9+1;
        rep(i,n){
            l = max(l, a[i].first);
            r = min(r, a[i].second);
            dp[i] = max(0, r - l + 1);
        }
    }
    int ans = 0;
    reverse(a.begin(), a.end());
    int l = -1, r = 1e9 +1;
    rep(i,n-1){
        l = max(l, a[i].first);
        r = min(r, a[i].second);
        ans = max(ans, max(0, r - l + 1) + dp[n-2-i]);
    }
    int ans2 = 0;
    {
        multiset<int>st, st2;
        rep(i,n)st.insert(-a[i].first);
        rep(i,n)st2.insert(a[i].second);
        rep(i,n){
            st.erase(st.find(-a[i].first));
            st2.erase(st2.find(a[i].second));
            int temp = a[i].second - a[i].first + 1;
            temp += max(0,(*st2.begin())+(*st.begin()) + 1);
            ans2 = max(ans2, temp);
            st.insert(-a[i].first);
            st2.insert(a[i].second);
        }
    }
    ans = max(ans, ans2);
    cout << ans << endl;
}


Measured memory_bytes: 10868