#include <bits/stdc++.h>
using namespace std;
int n, b[300100], c[300100];
pair<int, int> a[300100];
bool q;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    b[i] = a[i].first;
    c[i] = a[i].second;
  }
  sort(b, b + n);
  sort(c, c + n);
  int ans = max(0, c[0] - b[n - 1]);
  int u = 0, d = 0;
  if (b[n - 1] != b[n - 2])
    d = min(b[n - 1] - b[n - 2], max(0, c[0] - b[n - 2]));
  if (c[0] != c[1]) u = min(c[1] - c[0], max(0, c[1] - b[n - 1]));
  if (n > 1)
    for (int i = 0; i < n; i++) {
      if (a[i].first == b[n - 1] && a[i].second == c[0]) q = 1;
    }
  if (q)
    ans += u + d;
  else
    ans += max(u, d);
  cout << ans << endl;
}
