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);
    for i:=0 to N-1 do write(A[i],' '); writeln;
    for i:=0 to N-1 do write(D[i],' '); writeln;
    strettemano:=0;    if a[0]<d[0] then pp:=1
                                    else pp:=0;
    j:=0; i:=1; uscita:=false;
    
    while  i<N  do
               begin
                     write(a[i],' ',d[j],' ');
                    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;
               writeln(pp,' ',strettemano);
               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.
