program puzzle(input,output);
(* an undocumented, compute-bound program from forest baskett *)
const	size	=	511;	(* d*d*d - 1 *)
	classMax =	3;
	typeMax	=	12;
	d	=	8;

type	pieceClass =	0..classMax;
	pieceType =	0..typeMax;
	position =	0..size;

var	pieceCount :	array [pieceClass] of 0..13;
	class	:	array [pieceType] of pieceClass;
	pieceMax :	array [pieceType] of position;
	puzzle	:	array [position] of boolean;
	p	:	array [pieceType, position] of boolean;
	m,n	:	position;
	i,j,k	:	0..13;
	kount	:	integer;

function fit (i : pieceType; j : position) : boolean;

label	1;
var	k	:	position;

begin
	fit := false;
	for k := 0 to pieceMax[i] do
		if p[i,k] then if puzzle[j+k] then goto 1;
	fit := true;
1:
end;

function place (i : pieceType; j : position) : position;

label	1;
var	k	:	position;

begin
	for k := 0 to pieceMax[i] do
		if p[i,k] then puzzle[j+k] := true;
	pieceCount[class[i]] := pieceCount[class[i]] - 1;
	for k := j to size do
		if not puzzle[k] then begin
			place := k;
			goto 1;
		end;
	writeln('puzzle filled');
	place := 0;
1:
end;

procedure remove (i : pieceType; j : position);

var	k	:	position;

begin
	for k := 0 to pieceMax[i] do
		if p[i,k] then puzzle[j+k] := false;
	pieceCount[class[i]] := pieceCount[class[i]] + 1;
end;

function trial (j : position) : boolean;

label	1;

var	i	:	pieceType;
	k	:	position;

begin	for i := 0 to typeMax do
		if pieceCount[class[i]] <> 0 then 
			if fit (i, j) then begin
				k := place (i, j);
				if trial(k) or (k = 0) then begin
					writeln ('piece', i+1, ' at', k+1);
					trial := true;
					goto 1;
				end else remove (i, j);
			end;
	trial := false;
1:	kount := kount + 1;
end;

begin
	writeln('elapsed user time:',clock/1000:9:3);
	for m := 0 to size do puzzle[m] := true;
	for i := 1 to 5 do for j := 1 to 5 do for k := 1 to 5 do
		puzzle[i+d*(j+d*k)] := false;
	for i := 0 to typeMax do for m := 0 to size do p[i, m] := false;
	for i := 0 to 3 do for j := 0 to 1 do for k := 0 to 0 do
		p[0,i+d*(j+d*k)] := true;
	class[0] := 0;
	pieceMax[0] := 3+d*1+d*d*0;
	for i := 0 to 1 do for j := 0 to 0 do for k := 0 to 3 do
		p[1,i+d*(j+d*k)] := true;
	class[1] := 0;
	pieceMax[1] := 1+d*0+d*d*3;
	for i := 0 to 0 do for j := 0 to 3 do for k := 0 to 1 do
		p[2,i+d*(j+d*k)] := true;
	class[2] := 0;
	pieceMax[2] := 0+d*3+d*d*1;
	for i := 0 to 1 do for j := 0 to 3 do for k := 0 to 0 do
		p[3,i+d*(j+d*k)] := true;
	class[3] := 0;
	pieceMax[3] := 1+d*3+d*d*0;
	for i := 0 to 3 do for j := 0 to 0 do for k := 0 to 1 do
		p[4,i+d*(j+d*k)] := true;
	class[4] := 0;
	pieceMax[4] := 3+d*0+d*d*1;
	for i := 0 to 0 do for j := 0 to 1 do for k := 0 to 3 do
		p[5,i+d*(j+d*k)] := true;
	class[5] := 0;
	pieceMax[5] := 0+d*1+d*d*3;
	for i := 0 to 2 do for j := 0 to 0 do for k := 0 to 0 do
		p[6,i+d*(j+d*k)] := true;
	class[6] := 1;
	pieceMax[6] := 2+d*0+d*d*0;
	for i := 0 to 0 do for j := 0 to 2 do for k := 0 to 0 do
		p[7,i+d*(j+d*k)] := true;
	class[7] := 1;
	pieceMax[7] := 0+d*2+d*d*0;
	for i := 0 to 0 do for j := 0 to 0 do for k := 0 to 2 do
		p[8,i+d*(j+d*k)] := true;
	class[8] := 1;
	pieceMax[8] := 0+d*0+d*d*2;
	for i := 0 to 1 do for j := 0 to 1 do for k := 0 to 0 do
		p[9,i+d*(j+d*k)] := true;
	class[9] := 2;
	pieceMax[9] := 1+d*1+d*d*0;
	for i := 0 to 1 do for j := 0 to 0 do for k := 0 to 1 do
		p[10,i+d*(j+d*k)] := true;
	class[10] := 2;
	pieceMax[10] := 1+d*0+d*d*1;
	for i := 0 to 0 do for j := 0 to 1 do for k := 0 to 1 do
		p[11,i+d*(j+d*k)] := true;
	class[11] := 2;
	pieceMax[11] := 0+d*1+d*d*1;
	for i := 0 to 1 do for j := 0 to 1 do for k := 0 to 1 do
		p[12,i+d*(j+d*k)] := true;
	class[12] := 3;
	pieceMax[12] := 1+d*1+d*d*1;
	pieceCount[0] := 13;
	pieceCount[1] := 3;
	pieceCount[2] := 1;
	pieceCount[3] := 1;
	m := 1+d*(1+d*1);
	kount := 0;
	if fit(0, m) then n := place(0, m) else writeln('error 1');
	if trial(n) then writeln('success in', kount, ' trials')
	else writeln('failure');
        writeln('elapsed user time:',clock/1000:9:3);   
end.