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>
#define _ << ' ' <<
#define _squ(x)  ((x) * (x))
#define debug(var)  do{std::cerr << #var << " : "; view(var);}while(0)
#define line_debug(var)  do{std::cerr << "l" << __LINE__ << " " << #var << " : "; view(var);}while(0)
#define _max(v)  *max_element(v.begin(), v.end())
#define _min(v)  *min_element(v.begin(), v.end())
#define _sort(var)  std::sort(var.begin(), var.end());
#define _rsort(var)  std::sort(var.begin(), var.end(), std::greater<>());
using namespace std;
using ll = long long;
int gcd(int m, int n){return !n ? m : gcd(n, m%n);}
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;
template<typename T> void view(const T e){ std::cerr << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){ for(const auto& e : v) std::cerr << e << " "; std::cerr << std::endl; }
template<typename T> void view(const std::vector<std::vector<T>>& vv){ for(const auto& v : vv){ view(v); } }

//void to_bin (vector<bool>& conb, int deci){
//  for(size_t i=0; i<conb.size(); ++i){
//    if(deci%2==0) conb[i] = 0;
//    else conb[i] = 1;
//    deci /= 2;
//  }
//}

int main(){
  int i, j;
  int N;
  cin >> N;
  vector<pair<int, int>> pairs(N);
  for(i=0; i<N; ++i) cin >> pairs[i].first >> pairs[i].second;
  sort(pairs.begin(), pairs.end());
  int lower = INT_MAX;
  long long max_funny = 0;
  for(i=1; i<N; ++i){
    long long temp_funny = 0;
    lower = pairs[0].second;
    for(j=1; j<i; ++j){
      lower = min(lower, pairs[j].second);
    }
    long long temp = lower - pairs[i-1].first;
    temp_funny = (temp>0? temp : 0);
    lower = pairs[i].second;
    for(j=i+1; j<N; ++j){
      lower = min(lower, pairs[j].second);
    }
    temp = lower - pairs[N-1].first;
    temp_funny += (temp>0? temp : 0);
    max_funny = max(max_funny, temp_funny+2);
  }
//  for(const auto& e : pairs) cout << e.first << " " << e.second << "  ";
//  cout << endl;
  cout << max_funny << endl;


  /*
  V<int> l(N);
  V<int> r(N);
  int upper = 0;
  int lower = INT_MAX;
  for(i=0; i<l.size(); i++) cin >> l[i] >> r[i];
  unsigned long long max_funny = 0;
  unsigned long long deci = 0;
  vector<bool> conb(N);
  for(unsigned long long i=0; i<N*N; ++i){
    to_bin(conb, deci);
    deci++;
    upper = 0;
    lower = INT_MAX;
    for(unsigned long long i=0; i<conb.size(); ++i){
      if(conb[i]){
        upper = max(upper, l[i]);
        lower = min(lower, r[i]);
      }
    }
    if(upper==0 && lower==INT_MAX) continue;
    unsigned long long funny_temp = 0;
    if(lower>upper) funny_temp = lower - upper + 1;
    upper = 0;
    lower = INT_MAX;
    for(int i=0; i<conb.size(); ++i){
      if(!conb[i]){
        upper = max(upper, l[i]);
        lower = min(lower, r[i]);
      }
    }
    if(upper==0 && lower==INT_MAX) continue;
    if(lower>upper) funny_temp += lower - upper + 1;
    max_funny = max(max_funny, funny_temp);
  }
  cout << max_funny << endl;
  */
  return 0;
}