program project1;
 var n,i,j,bp:longint;
   a,b,h,v:array [1..1000000] of longint;
begin
  bp:=0;
  readln(n);

  for i:=1 to n do
  begin
    h[i]:=0;
    v[i]:=0;
  end;

  for i:=1 to n do begin
    read(a[i],b[i]);
      v[a[i]]:=v[a[i]]+1;
      h[b[i]]:=h[b[i]]+1;
  end;


    for i:=1 to n do begin
      if h[i]=0 then for j:=i+1 to n do begin
        if (h[j]<>0) and (h[i]=0) then begin
          h[i]:=h[i]+1;
          h[j]:=h[j]-1;
          bp:=bp+j-i;
        end;end;
    end;
    for i:=n downto 1 do begin
      if h[i]=0 then for j:=i-1 downto 1 do begin
        if (h[j]<>0) and(h[i]=0) then begin
          h[i]:=h[i]+1;
          h[j]:=h[j]-1;
          bp:=bp+i-j
        end;
      end;
    end;

     for i:=1 to n do begin
      if v[i]=0 then for j:=i+1 to n do begin
        if (v[j]<>0) and (v[i]=0) then begin
          v[i]:=v[i]+1;
          v[j]:=v[j]-1;
          bp:=bp+j-i;
        end;end;
    end;
    for i:=n downto 1 do begin
      if v[i]=0 then for j:=i-1 downto 1 do begin
        if (v[j]<>0) and (v[i]=0) then begin
          v[i]:=v[i]+1;
          v[j]:=v[j]-1;
          bp:=bp+i-j
        end;
      end;
    end;
     writeln(bp);
end.