#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <algorithm>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());


struct Node {
	Node(int x = -1e9) {
		// init
		ans = x;
	}
	
	Node(Node l, Node r) {
		// merge
		ans = std::max(l.ans, r.ans);
	}

	// atributes
	int ans;
};

template <class i_t, class e_t>
class SegmentTree {
public:
	void init(std::vector<e_t> base) {
		n = base.size();
		tree.resize(2 * n);
		for(int i = 0; i < n; i++) {
			tree[i + n] = i_t(base[i]);
		}
		for(int i = n - 1; i > 0; i--) {
			tree[i] = i_t(tree[i + i], tree[i + i + 1]);
		}
	}
	
	i_t qry(int l, int r) {
		i_t lp, rp;
		for(l += n, r += n; l < r; l /= 2, r /= 2) {
			if(l & 1) lp = i_t(lp, tree[l++]);
			if(r & 1) rp = i_t(tree[--r], rp);
		}
		return i_t(lp, rp);
	}
	
	void upd(int pos, e_t v) {
		pos += n;
		tree[pos] = i_t(v);
		for(pos /= 2; pos > 0; pos /= 2) {
			tree[pos] = i_t(tree[pos + pos], tree[pos + pos + 1]);
		}
	}
	
private:
	int n;
	std::vector<i_t> tree;
};

int solve(std::vector<std::pair<int, int>> ranges) {
	int n = (int) ranges.size();
	int ans = 0;
	// second case, take range with min r, it limits r for sure, max l limits r
	{
		std::sort(ranges.begin(), ranges.end());
		std::vector<int> a, b;
		for(auto p : ranges) {
			//std::cout << "[" << p.first << ", " << p.second << ")\n";
			a.push_back(p.first);
			b.push_back(-p.second);
		}
		SegmentTree<Node, int> A, B;
		A.init(a);
		B.init(b);
		for(int i = 0; i < n; i++) {
			//std::cout << -(A.qry(0, i).ans + B.qry(0, i).ans) << ' ' << -(A.qry(i, n).ans + B.qry(i, n).ans) << '\n';
			if(i != 0 && i+1 != n) ans = std::max(ans, std::max(0, -(A.qry(0, i).ans + B.qry(0, i).ans)) + std::max(0, -(A.qry(i, n).ans + B.qry(i, n).ans)));
			ans = std::max(ans, ranges[i].second - ranges[i].first + std::max(0, -(std::max(A.qry(0, i).ans, A.qry(i+1, n).ans) + std::max(B.qry(0, i).ans, B.qry(i+1, n).ans))));
		}
	}
	{
		for(auto &p : ranges) {
			std::swap(p.first, p.second);
		}
		std::sort(ranges.begin(), ranges.end());
		for(auto &p : ranges) {
			std::swap(p.first, p.second);
		}
		std::sort(ranges.begin() + 1, ranges.end());
		std::vector<int> a, b;
		for(auto p : ranges) {
			//std::cout << "[" << p.first << ", " << p.second << ")\n";
			a.push_back(p.first);
			b.push_back(-p.second);
		}
		SegmentTree<Node, int> A, B;
		A.init(a);
		B.init(b);
		for(int i = 1; i < n; i++) {
			//std::cout << -(A.qry(0, i).ans + B.qry(0, i).ans) << ' ' << -(A.qry(i, n).ans + B.qry(i, n).ans) << '\n';
			ans = std::max(ans, std::max(0, -(A.qry(0, i).ans + B.qry(0, i).ans)) + std::max(0, -(A.qry(i, n).ans + B.qry(i, n).ans)));
		}
	}
	return ans;
}

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int n;
	std::cin >> n;
	std::vector<std::pair<int, int>> ranges(n);
	int ans = 0;
	for(auto &p : ranges) {
		std::cin >> p.first >> p.second;
		p.first--;
		// first case
		ans = std::max(p.second - p.first, ans);
	}
	ans = std::max(ans, solve(ranges));
	for(auto &p : ranges) {
		std::swap(p.first, p.second);
		p.first = -p.first;
		p.second = -p.second;
	}
	ans = std::max(ans, solve(ranges));
	std::cout << ans << '\n';
}

Measured memory_bytes: 5052