Problem:
Score : 600 points

Problem Statement

10^9 contestants, numbered 1 to 10^9, will compete in a competition.
There will be two contests in this competition.

The organizer prepared N problems, numbered 1 to N, to use in these contests.
When Problem i is presented in a contest, it will be solved by all contestants from Contestant L_i to Contestant R_i (inclusive), and will not be solved by any other contestants.

The organizer will use these N problems in the two contests.
Each problem must be used in exactly one of the contests, and each contest must have at least one problem.

The joyfulness of each contest is the number of contestants who will solve all the problems in the contest.
Find the maximum possible total joyfulness of the two contests.

Constraints

2 \leq N \leq 10^5

1 \leq L_i \leq R_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the maximum possible total joyfulness of the two contests.

Sample Input 1

4
4 7
1 4
5 8
2 5

Sample Output 1

6

The optimal choice is:

Use Problem 1 and 3 in the first contest. Contestant 5, 6, and 7 will solve both of them, so the joyfulness of this contest is 3.

Use Problem 2 and 4 in the second contest. Contestant 2, 3, and 4 will solve both of them, so the joyfulness of this contest is 3.

The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.

Sample Input 2

4
1 20
2 19
3 18
4 17

Sample Output 2

34

Sample Input 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

Sample Output 3

540049931

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
// use gp_hash_table<int,int,custom_hash> m;
// in place of map
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define repba(i,b,a) for(int i=b;i>=a;--i)
#define repab(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define msi map<string,int>
#define mii map<int,int>
#define viip vector<pair<int,pair<int,int> > > 
#define mp make_pair
#define vip vector<pair<int,int> >
#define pb push_back 
#define po pop_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
// This custom_hash is for unordered_map 
// use map<int, int, custom_hash> mp;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
pair<int,int> p;
bool comp(const pair<int,int> &a,const pair<int,int> &b){
	if(a.sec > p.fi && b.sec > p.fi){
		return (a.fi < b.fi);
	}
	int o1;
	if(a.sec <= p.sec){
		o1 = a.sec - a.fi+1;
	}
	else {
		o1 = p.sec-a.fi+1;
	}
	int o2;
	if(b.sec <= p.sec){
		o2 = b.sec - b.fi+1;
	}
	else {
		o2 = p.sec-b.fi+1;
	}
	if(o1 < o2)return 0;
	return 1;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	// #ifndef ONLINE_JUDGE
 //    	freopen("input.txt", "r", stdin);
 //    	freopen("output.txt", "w", stdout);
	// #endif
	int n;
	cin>>n;
	vip v(n);
	int min1 = INT_MAX;
	rep(i,n){
		cin>>v[i].fi>>v[i].sec;
		if(min1 > v[i].fi){
			min1 = v[i].fi;
			swap(v[0].fi,v[i].fi);
			swap(v[0].sec,v[i].sec);
		}
	}
	vip v2(n-1);
	repa(i,1,n){
		v2[i-1]=v[i];
	}
	p = v[0];
	vi a(n);
	sort(all(v2),comp);
	// rep(i,s(v2)){
	// 	cout<<v2[i].fi<<" "<<v2[i].sec<<"\n";
	// }
	int l = v2[n-2].fi;
	int r = v2[n-2].sec;
	a[n-2] = r-l+1;
	repr(i,n-2){
		if(l>r)a[i]=0;
		if(v2[i].fi > r || l > v2[i].sec){
			l=r+1;
			a[i]=0;
		}
		else{
			l = max(l,v2[i].fi);
			r = min(r,v2[i].sec);
			a[i]=r-l+1;
		}
		// cout<<a[i]<<" ";
	}
	// rep(i,n-1){
	// 	cout<<a[i]<<" ";
	// }
	int maxlen = 0;
	l = v[0].fi;
	r = v[0].sec;
	repa(i,1,n){
		// cout<<l<<" "<<r<<" "<<a[i-1]<<"\n";
		maxlen = max(maxlen,r-l+1+a[i-1]);
		if(l>r)l=r+1;
		if(v2[i-1].fi > r || l > v2[i-1].sec){
			l=r+1;
			// a[i]=0;
		}
		else{
			l = max(l,v2[i-1].fi);
			r = min(r,v2[i-1].sec);
			// a[i]=r-l+1;
		}
	}
	cout<<maxlen;
	return 0;
}