#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string>vst;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef map<int,int> dct;

#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
#define all(ct) ct.begin() , ct.end()
#define endl "\n"
#define fr(i,a) for(auto i:a)
#define f(i,a,b) for(int i=a;i<b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define in(d,v) d.find(v)!=d.end()
#define mp make_pair

bool compare(pair<int,pii> a, pair<int,pii> b){
    if(a.first!=b.first)return a.first>b.first;
    return a.second<b.second;
}

const int mod = 1e9 + 7;
const ll inf = ll(1e18);

ll modexp(ll x,ll n,ll k){
    if(n==0)return k;
    if(n%2==0)return modexp((x*x)%mod,n/2,k);
    return (x*modexp((x*x)%mod,n/2,k))%mod;
}


int main(){
    fastio;
    //clock_t tm = clock();
    int n,l,r;
    cin>>n;
    vector<pair<int,pii>>a;
    f(i,0,n){
        cin>>l>>r;
        a.pb({r-l,{l,r}});
    }
    sort(all(a),compare);
    int l1,l2=1,r1,r2=1,lcp1,rcp1,lcp2,rcp2;
    l1=a[0].second.first;r1=a[0].second.second;
    int flag=0,ps=-1;
    f(i,1,n){
        if(a[i].second.first>r1){
            flag=1;ps=i;
            l2=a[i].second.first;r2=a[i].second.second;
            break;
        }
    }
    if(flag==1){
        fd(j, n-1, 1) {
            if(j==ps)continue;
            int tl1, tl2, tr1, tr2;
            l = a[j].second.first;
            r = a[j].second.second;
            tl1 = max(l1, l);
            tl2 = max(l2, l);
            tr1 = min(r1, r);
            tr2 = min(r2, r);
            if (max(0, tr1 - tl1 + 1) + max(0, r2 - l2 + 1) > max(0, tr2 - tl2 + 1) + max(0, r1 - l1 + 1)) {
                l1 = tl1;
                r1 = tr1;
            } else {
                l2 = tl2;
                r2 = tr2;
            }
        }
    }
    else{
        //int i = 1;
        //while (i < n - 1 and a[i] == a[i - 1])i++;
        l2 = a[n-1].second.first;
        r2 = a[n-1].second.second;
        fd(j, n-2, 1) {
            int tl1, tl2, tr1, tr2;
            l = a[j].second.first;
            r = a[j].second.second;
            tl1 = max(l1, l);
            tl2 = max(l2, l);
            tr1 = min(r1, r);
            tr2 = min(r2, r);
            if (max(0, tr1 - tl1 + 1) + max(0, r2 - l2 + 1) > max(0, tr2 - tl2 + 1) + max(0, r1 - l1 + 1)) {
                l1 = tl1;
                r1 = tr1;
            } else {
                l2 = tl2;
                r2 = tr2;
            }
        }
        lcp1=l1;rcp1=r1;lcp2=l2;rcp2=r2;
        l1=a[0].second.first;r1=a[0].second.second;
        int i = 1;
        while (i < n - 1 and a[i] == a[i - 1])i++;
        l2 = a[i].second.first;
        r2 = a[i].second.second;
        fd(j, n-1, i+1) {
            int tl1, tl2, tr1, tr2;
            l = a[j].second.first;
            r = a[j].second.second;
            tl1 = max(l1, l);
            tl2 = max(l2, l);
            tr1 = min(r1, r);
            tr2 = min(r2, r);
            if (max(0, tr1 - tl1 + 1) + max(0, r2 - l2 + 1) > max(0, tr2 - tl2 + 1) + max(0, r1 - l1 + 1)) {
                l1 = tl1;
                r1 = tr1;
            } else {
                l2 = tl2;
                r2 = tr2;
            }
        }
        if((max(0,r2-l2+1)+max(0,r1-l1+1))<(max(0,rcp2-lcp2+1)+max(0,rcp1-lcp1+1))){
            r2=rcp2;l2=lcp2;r1=rcp1;l1=lcp1;
        }
    }
    cout<<max(0,r2-l2+1)+max(0,r1-l1+1)<<endl;
    //cout<<((clock()-tm)*1.0)/CLOCKS_PER_SEC<<endl;
}

Measured memory_bytes: 1912