//na :cuixiaofei;
//da :08_04_24;
program rez;
var
n,max :longint;
a :array[-1..9000,-1..9010] of boolean;
f :array[-1..9010] of longint;
f1,f2 :text;
procedure init;
var
i,a1,a2 :longint;
begin
assign(f1,'rez.in');
reset(f1);
assign(f2,'rez.out');
rewrite(f2);
fillchar(a,sizeof(a),false);
fillchar(f,sizeof(f),0);
max:=0;
readln(f1,n);
for i:=1 to n do
begin
readln(f1,a1,a2);
a[a1,a2]:=true;
if a2>max then
max:=a2;
end;
end;
procedure main;
var
i,j,mmm :longint;
begin
for i:=0 to max do
begin
mmm:=0;
for j:=0 to i-1 do
begin
if a[j,i]=true then
begin
if f[j]+i-j>mmm then
mmm:=f[j]+i-j;
end
else
begin
if f[j]>mmm then
mmm:=f[j];
end;
end;
if mmm>f[i] then
f[i]:=mmm;
if f[i]<f[i-1] then
f[i]:=f[i-1];
end;
end;
procedure print;
begin
writeln(f2,f[max]);
close(f1);
close(f2);
end;
begin
init;
main;
print;
end.