{$apptype console}

uses
  Math, Types;

const
  INF = 500000;  // Max(capacity) is enough
  
var
  p, n: integer;
  q: TIntegerDynArray;
  s, d: array of TIntegerDynArray;
  mm, flow, rem: array of TIntegerDynArray;

function InOf(Machine: integer): integer;
begin
  Result := Machine * 2 + 1;  // нечетный
end;

function OutOf(Machine: integer): integer;
begin
  Result := Machine * 2 + 2;  // четный
end;

function DecodeIn(InIdx: integer): integer;
begin
  Result := (InIdx - 1) div 2 + 1;
end;

function DecodeOut(OutIdx: integer): integer;
begin
  Result := (OutIdx - 2) div 2 + 1;
end;

function IsProdLine(I, J: integer): boolean;
begin
  Result := (I mod 2 = 0) and (J mod 2 = 1) and (flow[I, J] > 0);
end;

function Compatible(I, J: integer): boolean;
var
  k: integer;
begin
  Result := true;
  for k := 0 to p - 1 do
    if (s[J, k] <> 2) and (d[I, k] <> s[J, k]) then begin
      Result := false;
      Break;
    end;
end;

var
  fi, fo: textfile;
  i, j, k: integer;
  isS, isD: boolean;
  reached: TBooleanDynArray;
  queue: TIntegerDynArray;
  qp, qc: integer;
  prior: TIntegerDynArray;
  minFlow, totalFlow: integer;
  cnt: integer;
begin
  Read(p, n);
  SetLength(s, n, p);
  SetLength(d, n, p);
  SetLength(q, n);
  for i := 0 to n - 1 do begin
    Read(q[i]);
    for j := 0 to p - 1 do
      Read(s[i, j]);
    for j := 0 to p - 1 do
      Read(d[i, j]);
  end;

  SetLength(mm, 2 * n + 2, 2 * n + 2);    // исходная матрица
  SetLength(flow, 2 * n + 2, 2 * n + 2);  // поток
  SetLength(rem, 2 * n + 2, 2 * n + 2);   // остаточная сеть
  for i := 0 to High(mm) do
    for j := 0 to High(mm[i]) do begin
      mm[i, j] := 0;
      flow[i, j] := 0;
    end;

  for i := 0 to n - 1 do
    mm[InOf(i), OutOf(i)] := q[i];   // станки

  // общий исток и общий сток -- вершины 0 и High(mm)
  for i := 0 to n - 1 do begin
    isS := true;
    isD := true;
    for k := 0 to p - 1 do begin
      if (s[i, k] = 1) then
        isS := false;
      if (d[i, k] = 0) then
        isD := false;
    end;
    if isS then
      mm[0, InOf(i)] := INF;
    if isD then
      mm[OutOf(i), High(mm)] := INF;
  end;

  // ребра между совместимыми станками
  for i := 0 to n - 1 do
    for j := 0 to n - 1 do
      if (i <> j) and Compatible(i, j) then
        mm[OutOf(i), InOf(j)] := INF;

  // изначально остаточная сеть равна исходной
  for i := 0 to High(mm) do
    for j := 0 to High(mm[i]) do
      rem[i, j] := mm[i, j];

  SetLength(reached, Length(mm));
  SetLength(queue, Length(mm));
  SetLength(prior, Length(mm));
  while true do begin
    // поиск в ширину
    for i := 0 to High(prior) do
      prior[i] := -1;
    FillChar(reached[0], Length(reached), false);
    reached[0] := true;
    qc := 1;
    queue[0] := 0;
    qp := 0;
    while (qp < qc) do begin
      for i := 0 to High(mm) do
        if (not reached[i]) and (rem[queue[qp], i] > 0) then begin
          reached[i] := true;
          prior[i] := queue[qp];
          queue[qc] := i;
          Inc(qc);
        end;
      Inc(qp);
    end;
    // есть дополняющий путь
    if reached[High(mm)] then begin
      // минимальное ребро доп. пути
      minFlow := INF;
      i := High(mm);
      while (prior[i] <> -1) do begin
        minFlow := Min(minFlow, rem[prior[i], i]);
        i := prior[i];
      end;
      Assert(minFlow <> INF);

      // модифицируем поток и остаточную сеть
      i := High(mm);
      while (prior[i] <> -1) do begin
        Dec(rem[prior[i], i], minFlow);
        Inc(rem[i, prior[i]], minFlow);

        if (mm[prior[i], i] <> 0) then
          Inc(flow[prior[i], i], minFlow);
        if (mm[i, prior[i]] <> 0) then
          Dec(flow[i, prior[i]], minFlow);
        i := prior[i];
      end;
    end
    else
      Break;
  end;

  totalFlow := 0;
  for i := 1 to High(flow) - 1 do
    Inc(totalFlow, flow[0, i]);

  cnt := 0;
  for i := 1 to High(flow) - 1 do
    for j := 1 to High(flow) - 1 do
      if IsProdLine(i, j) then
        Inc(cnt);

  Write(totalFlow, ' ', cnt);
  for i := 1 to High(flow) - 1 do
    for j := 1 to High(flow) - 1 do
      if IsProdLine(i, j) then
        Write(' ', DecodeOut(i), ' ', DecodeIn(j), ' ', flow[i, j]);

end.
