var
a,b:array[0..5100] of longint;
s,t,n,ans1,ans2,i,j:longint;
function max(x,y:longint):longint;
begin
if x<y then exit(y) else exit(x);
end;
procedure Qsort(x,y:longint);
var mid,i,j,t:longint;
begin
i:=x; j:=y; mid:=a[(x+y) div 2];
repeat
while a[i]<mid do inc(i);
while a[j]>mid do dec(j);
if i<=j then
begin
t:=a[i]; a[i]:=a[j]; a[j]:=t;
t:=b[i]; b[i]:=b[j]; b[j]:=t;
inc(i); dec(j);
end;
until i>j;
if x<j then Qsort(x,j);
if i<y then Qsort(i,y);
end;
begin
readln(n);
for i:=1 to n do read(a[i],b[i]);
Qsort(1,n);
ans1:=b[1]-a[1]; s:=a[1]; t:=b[1];
for i:=2 to n do
begin
if b[i]<=t then continue;
if (b[i]>t)and(a[i]<=t) then
begin
ans1:=max(ans1,b[i]-s);
t:=b[i];
continue;
end;
if a[i]>t then
begin
ans2:=max(ans2,a[i]-t);
s:=a[i]; t:=b[i];
end;
end;
writeln(ans1,‘ ‘,ans2);
end.
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1000050];
int main(){
int n,x,y,j,i,x1=0,x2=0,s=1,s2=0,b=1111111111,e=0;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d%d",&x,&y);
b=min(b,x); e=max(e,y);
for(j=x;j<y;j++)a[j]=1;
}
for(i=b+1;i<=e;i++){
if(a[i]==1&&a[i-1]==1)s++;
if(a[i]==0&&a[i-1]==1){x1=max(x1,s);s=0;s2=1;}
if(a[i]==0&&a[i-1]==0)s2++;
if(a[i]==1&&a[i-1]==0){x2=max(x2,s2);s2=0;s=1;}
}
printf("%d %d\n",x1,x2);
return 0;
}