program df;
var
f1,f2:text;
a,b:array[1..10001]of longint;
n,y,x,i,m,j,k,g,h:longint;
begin
assign(f1,'punch.in');
assign(f2,'punch.out');
reset(f1);
rewrite(f2);
readln(f1,n);
j:=1;for i:= 1to n do a[i]:=-1;
for i:= 1to n do begin readln(f1,g,h);
while a[j]>=g do begin inc(k);b[k]:=a[j];a[j]:=-1;dec(j);end;
inc(j);a[j]:=g;
inc(j);a[j]:=h;
inc(m);x:=x mod 2;
for y:= k downto 1 do begin
if b[y]<=h then begin b[y]:=-1;inc(x);end
else begin inc(j);a[j]:=b[y];b[y]:=-1;end;
end;
m:=m-x div 2;
k:=0;
end;
writeln(f2,m);
close(f1);
close(f2);
end.