program mehmooni;
const
MAXN = 200000;
type elenco=array [0..MAXN] of longint;
var
N, i, j, h, pp : longint; (*pp=persone presenti*)
A, D : elenco;
strettemano: int64;
uscita:boolean;
procedure scambia (var x,y: longint);
var t:longint;
begin
t:=x;
x:=y;
y:=t;
end;
Procedure ordinamento (estremoi,estremos: longint; var v : elenco; ordinato:boolean);
var inf, sup, medio:longint;
pivot :longint;
begin
inf:=estremoi;
sup:=estremos;
medio:= (estremoi+estremos) div 2;
pivot:=v[medio];
repeat
if (ordinato) then
begin
while (v[inf]<pivot) do inf:=inf+1;
while (v[sup]>pivot) do sup:=sup-1;
end;
if inf<=sup then
begin
scambia(v[inf],v[sup]);
inf:=inf+1;
sup:=sup-1;
end;
until inf>sup;
if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
end;
begin
{
uncomment the following lines if you want to read/write from files
assign(input, 'input.txt'); reset(input);
assign(output, 'output.txt'); rewrite(output);
}
readln(N);
for i:=0 to N-1 do
readln(A[i], D[i]);
ordinamento (0,N-1,A, true);
ordinamento (0,N-1,D, true);
strettemano:=0; if a[0]<d[0] then pp:=1
else pp:=0;
j:=0; i:=1;
while i<N do
if (a[i]<d[j]) then begin strettemano:=strettemano + pp; pp:=pp+1; i:=i+1; end
else begin pp:=pp-1; strettemano:=strettemano + pp; j:=j+1;end;
if i=N then if j<N then for h:=j to N-1 do begin pp:=pp-1; strettemano:=strettemano + pp; end;
strettemano:=strettemano+2*N; (*tutti salutano il padrone di casa quando arrivanoe quando escono*)
writeln(strettemano);
end.
cHJvZ3JhbSBtZWhtb29uaTsKY29uc3QKICAgIE1BWE4gPSAyMDAwMDA7CnR5cGUgZWxlbmNvPWFycmF5IFswLi5NQVhOXSBvZiBsb25naW50Owp2YXIKICAgIE4sIGksIGosIGgsIHBwICA6IGxvbmdpbnQ7ICAoKnBwPXBlcnNvbmUgcHJlc2VudGkqKQogICAgQSwgRCAgOiBlbGVuY287CiAgICB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