Program Depot;

Const MAXPTS = 100000;
	NOLINE = -1;

Type
	TPoint = Record X, Y: Integer; End;

Var Pts: Array [1..MAXPTS] of TPoint;
	NumPts: Integer;
	Bad: Boolean;

{compare two points - we have to sort them for faster searching;
return true iff (A <= B)}
Function Compare (A, B: TPoint) : Boolean;
Begin
	If (A.X < B.X) then Compare := True
	else If (A.X > B.X) then Compare := False
	else Compare := (A.Y <= B.Y);
End;

Function Middle (A, B, C: TPoint) : TPoint;
Begin
	If Compare (A, B) and Compare (B, C) then Middle := B
	else If Compare (C, B) and Compare (B, A) then Middle := B
	else If Compare (A, C) and Compare (C, B) then Middle := C
	else If Compare (B, C) and Compare (C, A) then Middle := C
	else Middle := A;
End; { Middle }

{sorting function - not the best implementation but should be ok for us}
Procedure QuickSort (L, R: Integer);
Var LX, RX: Integer;
	Med, X: TPoint;
Begin
	LX := L; RX := R;
	If (L < R) then
	Begin
		Med := Pts[(L+R) div 2];
		If not Compare (Med, Pts[R]) then Med := Pts[R];
		If (R-L > 15) then
			Med := Middle (Pts[L+3], Pts[R-3], Pts[(L+R) div 2 + 2]);
		While (L < R) do
		Begin
			While (L < R) and Compare (Pts[L], Med) do Inc (L);
			While (L < R) and not Compare (Pts[R], Med) do Dec (R);
			If (L < R) then
			Begin
				X := Pts[L]; Pts[L] := Pts[R]; Pts[R] := X;
			End;
		End;
		if Compare (Pts[L], Med) and (L < RX) then
			Inc (L)
		else
			Dec (R);
		QuickSort (LX, R);
		QuickSort (L, RX);
	End;
End;

Function Solve : Integer;
Var I: Integer;
	R: Integer;
	X, Y: Integer;
Begin
	QuickSort (1, NumPts);
	R := 0;
	Y := NOLINE;
	For I := 1 to NumPts do
		If (Y = NOLINE) then
		Begin
			X := Pts[I].X;
			Y := Pts[I].Y;
		End else Begin
			If (X <> Pts[I].X) then Bad := true
			else R := R + (Pts[I].Y - Y);
			Y := NOLINE;
		End;
	Solve := R;
End;

Var I, Tmp, Res: Integer;

Begin
	Repeat
		ReadLn (NumPts);
		If (NumPts > 0) then
		Begin
			Bad := false;
			For I := 1 to NumPts do
				ReadLn (Pts[I].X, Pts[I].Y);
			Res := Solve;
			For I := 1 to NumPts do
			Begin
				Tmp := Pts[I].X;
				Pts[I].X := Pts[I].Y;
				Pts[I].Y := Tmp;
			End;
			Res := Res + Solve;
			If Bad then WriteLn ('The fence cannot be built.')
			else WriteLn ('The length of the fence will be ', Res, ' units.');
		End;
	until (NumPts = 0);
End.

