program balance;
type
rec=record
id,x:longint;
end;
var
n,i,j,ans:longint;
data:array[1..50000]of rec;
f:array[0..50000]of longint;
procedure swap(var a,b:rec);
var
t:rec;
begin
t:=a;
a:=b;
b:=t;
end;
procedure qsort(l,r:longint);
var i,j,mid:longint;
begin
i:=l;j:=r; mid:=data[(l+r) div 2].x;
repeat
while data[i].x<mid do inc(i);
while data[j].x>mid do dec(j);
if i<=j then begin
swap(data[i],data[j]);
inc(i);dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;{sort}
procedure init;
begin
assign(input,'balance.in');
reset(input);
assign(output,'balance.out');
rewrite(output);
readln(n);
for i:=1 to n do
begin
readln(data[i].id,data[i].x);
end;
qsort(1,n);
f[0]:=0;
if data[1].id=1 then f[1]:=1 else f[1]:=0;
for i:=2 to n do
begin
f[i]:=f[i-1]+data[i].id;
end;
end;
procedure closef;
begin
close(input);
close(output);
end;
procedure main;
begin
ans:=-1;
for i:=1 to n do
for j:=i to n do
begin
if f[j]-f[i-1]=(j-i+1)-(f[j]-f[i-1]) then
if data[j].x-data[i].x>ans then
ans:=data[j].x-data[i].x;
end;
writeln(ans);
end;
begin
init;
main;
closef;
end.