
{$R-,Q-}

program Block_Problem;

const
  name0          =            'types.in';
  name1          =            'block.in';
  name2          =            'block.out';
  ano            :            array[1..3, 1..2] of integer
                 =            ((2, 3), (1, 3), (1, 2));

type
  Tarr3          =            array[1..3] of integer;
  Tbl            =            array[1..4] of Tarr3;
  Tpart          =            object
                                num : integer;
                                bl : Tbl;
                                procedure Fix(t : byte);
                                procedure Sort_Cut;
                                function Same(p2 : Tpart) : boolean;
                              end;

procedure Tpart.Fix;
var temp : Tbl;
    i : integer;
begin
  for i := 1 to num do
    begin
      temp[i, t] := bl[i, t];
      temp[i, ano[t, 1] ] := bl[i, ano[t, 2] ];
      temp[i, ano[t, 2] ] := 5 - bl[i, ano[t, 1] ];
    end;
  bl := temp;
end;

procedure Tpart.Sort_Cut;
var temp : Tarr3;
    i, j : integer;
begin
  for i := 1 to num do
    for j := i+1 to num do
      if (bl[j, 1] < bl[i, 1]) or
         (bl[j, 1] = bl[i, 1]) and (bl[j, 2] < bl[i, 2]) or
         (bl[j, 1] = bl[i, 1]) and (bl[j, 2] = bl[i, 2]) and (bl[j, 3] < bl[i, 3])
      then begin
             temp := bl[i];
             bl[i] := bl[j];
             bl[j] := temp;
           end;
  for i := num downto 1 do
    begin
      dec(bl[i, 1], bl[1, 1]);
      dec(bl[i, 2], bl[1, 2]);
      dec(bl[i, 3], bl[1, 3]);
    end;
end;

function Tpart.same;
var i, j : integer;
begin
  same := false;
  if num <> p2.num then exit;
  for i := 1 to num do
    for j := 1 to 3 do
      if bl[i, j] <> p2.bl[i, j] then exit;
  same := true;
end;

const
  maxV  =  51;

var
  V, rV       :  integer;
  cube, fill  :  array[-7..14, -7..14, -7..14] of integer;
  parts       :  array[1..12, 1..24] of Tpart;
  tt          :  array[1..12] of integer;

  ansNeed, Need     :  integer;
  ansKind, Kind     :  array[1..12] of integer;

  expect            :  array[0..maxV] of integer;

procedure ready;
var inf : text;
    i, j, x, y, z : integer;
    te0, te1, te2 : Tpart;

  procedure get4;
  var k, l : integer;
      already : boolean;
  begin
    for k := 1 to 4 do
      begin
        te1.Fix(3);
        te2 := te1;
        te2.sort_cut;
        already := false;
        for l := 1 to tt[i] do
          if te2.same(parts[i, l]) then already := true;
        if not already then
          begin
            inc(tt[i]);
            parts[i, tt[i]] := te2;
          end;
      end;
  end;

begin
  fillchar(parts, sizeof(parts), 0);

  assign(inf, name0);
  reset(inf);
  for i := 1 to 12 do
    with parts[i, 1] do
      begin
        read(inf, j);
        read(inf, num);
        for j := 1 to parts[i, 1].num do
          read(inf, bl[j, 1], bl[j, 2], bl[j, 3]);
      end;
  close(inf);

  for i := 1 to 12 do
    begin
      te0 := parts[i, 1];
      parts[i, 1].sort_cut;
      tt[i] := 1;

      te1 := te0;
      get4;

      te1 := te0;
      te1.Fix(2);
      get4;

      te1 := te0;
      te1.Fix(2);
      te1.Fix(2);
      get4;

      te1 := te0;
      te1.Fix(2);
      te1.Fix(2);
      te1.Fix(2);
      get4;

      te1 := te0;
      te1.Fix(1);
      get4;

      te1 := te0;
      te1.Fix(1);
      te1.Fix(1);
      te1.Fix(1);
      get4;
    end;
end;


{=============ready=============}


procedure init;
var inf : text;
    i, x, y, z : integer;
begin
  fillchar(cube, sizeof(cube), 0);
  fillchar(fill, sizeof(fill), 0);
  assign(inf, name1);
  reset(inf);
  readln(inf, V);
  for i := 1 to V do
    begin
      read(inf, x, y, z);
      cube[x, y, z] := 1;
    end;
  close(inf);
end;

procedure doing(x0, y0, z0 : integer);
var i, j, k, x, y, z : integer;
    pass    : boolean;

begin
  if Need + expect[rV] >= ansNeed then exit;

  repeat
    inc(z0);
    if z0 > 7 then
      begin
        z0 := 1;
        inc(y0);
        if y0 > 7 then
          begin
            y0 := 1;
            inc(x0);
            if x0 > 7 then
              begin
{                if Need = ansNeed then
                  begin
                    for i := 1to 12 do
                      for j := 1 to Kind[i] do
                        write(i, ' ');
                    writeln;
                  end;}
                if Need < ansNeed then
                  begin
                    ansNeed := Need;
                    ansKind := Kind;
                  end;
                exit;
              end;
          end;
      end;
  until (cube[x0, y0, z0] = 1) and (fill[x0, y0, z0] = 0);

  for i := 12 downto 1 do
    for j := 1 to tt[i] do
      begin
        pass := true;
        for k := 1 to parts[i, j].num do
          begin
            x := x0 + parts[i, j].bl[k, 1];
            y := y0 + parts[i, j].bl[k, 2];
            z := z0 + parts[i, j].bl[k, 3];
            if (cube[x, y, z] = 0) or (fill[x, y, z] = 1) then
              begin
                pass := false;
                break;
              end;
          end;

        if pass then
          begin
            inc(Kind[i]);
            inc(Need);
            dec(rV, parts[i, j].num);

            for k := 1 to parts[i, j].num do
              fill[ x0 + parts[i, j].bl[k, 1],
                    y0 + parts[i, j].bl[k, 2],
                    z0 + parts[i, j].bl[k, 3] ] := 1;

            Doing(x0, y0, z0);

            for k := 1 to parts[i, j].num do
              fill[ x0 + parts[i, j].bl[k, 1],
                    y0 + parts[i, j].bl[k, 2],
                    z0 + parts[i, j].bl[k, 3] ] := 0;

            inc(rV, parts[i, j].num);
            dec(Need);
            dec(Kind[i]);
          end;

      end;
end;

procedure main;
var i : integer;
begin
  ansNeed := maxint;
  Need := 0;
  fillchar(kind, sizeof(kind), 0);
  rV := V;
  for i := 0 to V do
    if i mod 4 = 0 then expect[i] := i div 4
      else expect[i] := i div 4 + 1;
  doing(0, 0, 0);
end;

procedure out;
var ouf  : text;
    i, j : integer;
    bo : boolean;
begin
  assign(ouf, name2);
  rewrite(ouf);
  writeln(ouf, ansNeed);
  bo := false;
  for i := 1 to 12 do
    for j := 1 to ansKind[i] do
      begin
        if bo then write(ouf, ' ');
        bo := true;
        write(ouf, i);
      end;
  writeln(ouf);
  close(ouf);
end;

begin
  ready;
  init;
  main;
  out;
end.
