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)

#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <limits>
#include <ctime>
#include <cassert>
#include <map>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <stack>
#include <queue>
#include <numeric>
#include <iterator>
#include <bitset>
#include <unordered_map>
#include <unordered_set>

using namespace std;
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())
#define ten(n) ((int)1e##n)
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

template<typename ...> static inline int getchar_unlocked(void) { return getchar(); }
template<typename ...> static inline void putchar_unlocked(int c) { putchar(c); }
#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
void reader(int& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k && k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; }
void reader(ll& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k && k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; }
int reader(char c[]) { int i, s = 0; for (;;) { mygc(i); if (i != ' ' && i != '\n' && i != '\r' && i != '\t' && i != EOF) break; }c[s++] = i; for (;;) { mygc(i); if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF) break; c[s++] = i; }c[s] = '\0'; return s; }
int reader(string& c) { int i; for (;;) { mygc(i); if (i != ' ' && i != '\n' && i != '\r' && i != '\t' && i != EOF) break; }c.push_back(i); for (;;) { mygc(i); if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF) break; c.push_back(i); }; return sz(c); }
template <class T, class S> void reader(T& x, S& y) { reader(x); reader(y); }
template <class T, class S, class U> void reader(T& x, S& y, U& z) { reader(x); reader(y); reader(z); }
template <class T, class S, class U, class V> void reader(T& x, S& y, U& z, V& w) { reader(x); reader(y); reader(z); reader(w); }
void writer(int x, char c) { int s = 0, m = 0; char f[10]; if (x < 0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); }
void writer(ll x, char c) { int s = 0, m = 0; char f[20]; if (x < 0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); }
void writer(const char c[]) { int i; for (i = 0; c[i] != '\0'; i++)mypc(c[i]); }
void writer(const string& x, char c) { int i; for (i = 0; x[i] != '\0'; i++)mypc(x[i]); mypc(c); }
void writer(const char x[], char c) { int i; for (i = 0; x[i] != '\0'; i++)mypc(x[i]); mypc(c); }
template<class T> void writerLn(T x) { writer(x, '\n'); }
template<class T, class S> void writerLn(T x, S y) { writer(x, ' '); writer(y, '\n'); }
template<class T, class S, class U> void writerLn(T x, S y, U z) { writer(x, ' '); writer(y, ' '); writer(z, '\n'); }
template<class T, class S, class U, class V> void writerLn(T x, S y, U z, V v) { writer(x, ' '); writer(y, ' '); writer(z, ' '); writer(v, '\n'); }
template<class T> void writerArr(T x[], int n) { if (!n) { mypc('\n'); return; }FOR(i, n - 1)writer(x[i], ' '); writer(x[n - 1], '\n'); }
template<class T> void writerArr(vector<T>& x) { writerArr(x.data(), (int)x.size()); }

template<class T> void chmin(T& a, const T& b) { if (a > b) a = b; }
template<class T> void chmax(T& a, const T& b) { if (a < b) a = b; }

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
ll mod_pow(ll a, ll n, ll mod) {
	ll ret = 1;
	ll p = a % mod;
	while (n) {
		if (n & 1) ret = ret * p % mod;
		p = p * p % mod;
		n >>= 1;
	}
	return ret;
}
template<class T> T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; }
ll mod_inv(ll a, ll m) { ll x, y; extgcd<ll>(a, m, x, y); return (m + x % m) % m; }

#ifdef _MSC_VER
// #ifdef _DEBUG
template <typename ... Args>
void debugPrintf(const char* format, Args const& ... args) {
	fprintf(stderr, format, args ...);
	fflush(stderr);
}
#else
#define debugPrintf(...)
#endif

class sparse_tableMAX {
	vector<vector<int>> dat;
	vector<int> logTable;
	int n;

	int zero_element() const {
		return numeric_limits<int>::min();
	}
	int prop(int a, int b) const {
		return max(a, b);
	}

public:
	sparse_tableMAX(vector<int>& a) {
		n = sz(a);
		logTable.resize(n + 1);
		logTable[0] = logTable[1] = 0;
		for (int i = 2; i <= n; i++) logTable[i] = logTable[i >> 1] + 1;

		int logN = logTable[n];
		dat.resize(logN + 1, vector<int>(n, zero_element()));
		FOR(i, n) dat[0][i] = a[i];
		for (int i = 1; i <= logN; i++) {
			int l = 1 << i;
			for (int j = 0; j + l <= n; j++) {
				dat[i][j] = prop(dat[i - 1][j], dat[i - 1][j + l / 2]);
			}
		}
	}

	int query(int l, int r) {
		if (l >= r) {
			return zero_element();
		}
		int k = logTable[r - l];
		return prop(dat[k][l], dat[k][r - (1 << k)]);
	}

};

class sparse_tableMIN {
	vector<vector<int>> dat;
	vector<int> logTable;
	int n;

	int zero_element() const {
		return numeric_limits<int>::max();
	}
	int prop(int a, int b) const {
		return min(a, b);
	}

public:
	sparse_tableMIN(vector<int>& a) {
		n = sz(a);
		logTable.resize(n + 1);
		logTable[0] = logTable[1] = 0;
		for (int i = 2; i <= n; i++) logTable[i] = logTable[i >> 1] + 1;

		int logN = logTable[n];
		dat.resize(logN + 1, vector<int>(n, zero_element()));
		FOR(i, n) dat[0][i] = a[i];
		for (int i = 1; i <= logN; i++) {
			int l = 1 << i;
			for (int j = 0; j + l <= n; j++) {
				dat[i][j] = prop(dat[i - 1][j], dat[i - 1][j + l / 2]);
			}
		}
	}

	int query(int l, int r) {
		if (l >= r) return zero_element();
		int k = logTable[r - l];
		return prop(dat[k][l], dat[k][r - (1 << k)]);
	}

};


int main() {
	int n; reader(n);
	vector<Pii> lr(n);
	FOR(i, n) reader(lr[i].first, lr[i].second);
	// random_shuffle(lr.begin(), lr.end());
	sort(lr.begin(), lr.end());
	lr.erase(unique(lr.begin(), lr.end()), lr.end());
	n = sz(lr);
	if (n == 1) {
		int ans = lr[0].second - lr[0].first + 1;
		writerLn(ans * 2);
		return 0;
	}

	map<int, int> mpx, mpy;
	for (auto& xy : lr) {
		int x, y; tie(x, y) = xy;
		if (mpx.count(x) == 0) mpx[x] = y;
		else chmin(mpx[x], y);
		if (mpy.count(y) == 0) mpy[y] = x;
		else chmax(mpy[y], x);
	}

	vector<int> smx, smn;
	FOR(i, n) smx.push_back(lr[i].first);
	FOR(i, n) smn.push_back(lr[i].second);

	sparse_tableMAX smax(smx);
	sparse_tableMIN smin(smn);

	int ans = 0;
	FOR(i, n) {
		int len1 = lr[i].second - lr[i].first + 1;
		int l2 = max(smax.query(0, i), smax.query(i + 1, n));
		int r2 = min(smin.query(0, i), smin.query(i + 1, n));
		int len2 = r2 - l2 + 1;
		ans = max(ans, len1 + max(len2, 0));
	}

	int xmx = ten(9) + 20;
	for (auto it = mpx.rbegin(); it != mpx.rend(); ++it) {
		chmin(xmx, it->second);
		it->second = xmx;
	}
	int ymn = -2;
	for (auto it = mpy.begin(); it != mpy.end(); ++it) {
		chmax(ymn, it->second);
		it->second = ymn;
	}

	FOR(i, n) {
		if (i == 0) continue;
		int l1 = max(lr[0].first, lr[i].first);
		int r1 = min(lr[0].second, lr[i].second);
		int len1 = r1 - l1 + 1;

		int l2 = max(smax.query(1, i), smax.query(i + 1, n));
		int r2 = min(smin.query(1, i), smin.query(i + 1, n));

		auto it1 = mpx.lower_bound(l1 + 1);
		auto it2 = mpy.lower_bound(r1);
		it2--;
		int len2 = 0;
		if (it1 == mpx.end() && it2 == mpy.end()) {
			continue;
		} else if (it1 != mpx.end() && it2 != mpy.end()) {
			len2 = r2 - l2 + 1;
		} else if (it1 != mpx.end()) {
			len2 = it1->second - l2 + 1;
		} else {
			len2 = r2 - it2->second + 1;
		}

		len1 = max(len1, 0);
		len2 = max(len2, 0);

		ans = max(ans, len1 + len2);
	}
	writerLn(ans);

	return 0;
}