{$APPTYPE CONSOLE}
{$R+}
var
  f: TextFile;
  i, w, d, q, w1, w2, p, pw, delta, bestDelta, sum, bestSum, N, bestp, bestq, d1, d2: Integer;
  curSch, bestSch: array [1..4, 1..7] of Integer;
  H: array [1..52, 1..7] of Integer;
  b: Boolean;

begin
{  AssignFile(f, 'input.txt'); Reset(f);}
  Read(N);
  w1 := 53; w2 := 0;
  for i := 1 to N do begin
    Read(w, d, q);
    H[w, d] := q;
    if w < w1 then w1 := w;
    if w > w2 then w2 := w;
  end;
  d1 := 1;
  while d1 < 7 do begin
    if H[w1, d1] > 0 then break;
    Inc(d1);
  end;
  d2 := 7;
  while d2 > 1 do begin
    if H[w2, d2] > 0 then break;
    Dec(d2);
  end;
//  CloseFile(f);
  bestSum := MaxInt; bestp := 0;
  for p := 1 to 4 do begin
    sum := 0;
    for d := 1 to 7 do
      for pw := 1 to p do begin
        bestDelta := MaxInt; bestq := 0;
        for q := 0 to 100 do begin
          delta := 0;
          i := w1 + pw - 1;
          while i <= w2 do begin
            b := (H[i, d] <> q) and
              ((i > w1) or (d >= d1)) and
              ((i < w2) or (d <= d2));
            Inc(delta, Ord(b));
            Inc(i, p);
          end;
          if delta < bestDelta then begin
            bestDelta := delta;
            bestq := q;
          end;
        end;
        Inc(sum, bestDelta); 
        curSch[pw, d] := bestq;
      end;
    if sum < bestSum then begin
      bestSum := sum;
      bestSch := curSch;
      bestp := p;
    end;
  end;
//  AssignFile(f, 'output.txt'); Rewrite(f);
  Writeln(bestp);
  for pw := 1 to bestp do begin
    for d := 1 to 7 do
      Write(bestSch[pw, d], ' ');
    Writeln();
  end;
//  CloseFile(f);
end.
