{$O-,Q+,R+}
const MaxN=1000;
      MaxV=1000;
var a, l, r:array [1..MaxN] of integer;
    cnt:int64;
    i, j, k, m:integer;
    flag:boolean;

begin
  reset (input, 'cycles.in');
  rewrite (output, 'cycles.out');
  read (m);
  for i:=1 to m do begin
    read (l[i], r[i]);
    if l[i]<0 then a[i]:=a[-l[i]] else a[i]:=l[i];
  end;
  cnt:=0;
  repeat
    flag:=false;
    for j:=1 to m do if a[j]>r[j] then flag:=true;
    if not flag then inc (cnt);
    k:=0;
    for j:=m downto 1 do 
      if a[j]<r[j] then begin inc (a[j]); k:=j; break end;
    if k=0 then break;
    for j:=k+1 to m do 
      if l[j]<0 then a[j]:=a[-l[j]] else a[j]:=l[j];
  until false;
  writeln (cnt);
end.