// (C) Timofey S. Chistyakov 2005

{$apptype console}

uses
  Math, Types, SysUtils;

const
  MAX_ORDER = 100;

type
  TInt100Arr = array [0..MAX_ORDER] of integer;

  TSchedule = record
    Period: integer;
    Qtys: TIntegerDynArray;
    Conformed: integer;
  end;

var
  n: integer;
  orders: array of record
    Week, Day, Qty: integer;
  end;
  table: TIntegerDynArray;
  minDay, maxDay: integer;

procedure Done(const Msg: string; Res: integer);
begin
  Writeln(Msg);
  Halt(Res);
end;

function dayNo(Week, Day: integer): integer;
begin
  Result := (Week - 1) * 7 + (Day - 1);
end;

function periodDayNo(DayIdx, MinDay, Len: integer): integer;
var
  dayNo, minW, relDay: integer;
begin
  minW := MinDay div 7;
  dayNo := MinDay + DayIdx;
  relDay := dayNo - minW * 7;
  Result := relDay mod (Len * 7);
end;

function getConformed(const AFileType: string; var f: TextFile): integer;
var
  schedule: TSchedule;
  i: integer;
  p: array of TInt100Arr;
begin
  Read(f, schedule.Period);
  if not InRange(schedule.Period, 1, 4) then
    Done(AFileType + ': invalid period = ' + IntToStr(schedule.Period), 2);
  SetLength(schedule.Qtys, schedule.Period * 7);
  for i := 0 to High(schedule.Qtys) do
    if SeekEof(f) then
      Done(AFileType + ': unexpected eof', 2)
    else begin
      Read(f, schedule.Qtys[i]);
      if not InRange(schedule.Qtys[i], 0, 100) then
        Done(AFileType + ': invalid qty ' + IntToStr(schedule.Qtys[i]), 2);
    end;

  SetLength(p, schedule.Period * 7);
  for i := 0 to High(p) do
    FillChar(p[i], SizeOf(p[i]), 0);
  for i := 0 to High(table) do
    Inc(p[periodDayNo(i, minDay, schedule.Period), table[i]]);

  Result := 0;
  for i := 0 to High(p) do
    Inc(Result, p[i, schedule.Qtys[i]]);
end;

var
  fi, fo, fa: textfile;
  i: integer;
  aConf, oConf: integer;
begin
  AssignFile(fi, Paramstr(1));
  AssignFile(fo, Paramstr(2));
  AssignFile(fa, Paramstr(3));
  try
    {$I-}
    Reset(fi);
    Reset(fo);
    Reset(fa);
    if (IOResult <> 0) then
      Abort;
    {$I+}

    Read(fi, n);
    SetLength(orders, n);
    minDay := 1000;
    maxDay := -1;
    for i := 0 to n - 1 do
      with orders[i] do begin
        Read(fi, Week, Day, Qty);
        minDay := Min(minDay, dayNo(Week, Day));
        maxDay := Max(maxDay, dayNo(Week, Day));
      end;

    SetLength(table, maxDay - minDay + 1);
    FillChar(table[0], SizeOf(table[0]) * Length(table), 0);
    for i := 0 to n - 1 do
      with orders[i] do
        table[dayNo(Week, Day) - minDay] := Qty;

    aConf := getConformed('Answer', fa);
    oConf := getConformed('Output', fo);

    if (aConf = oConf) then
      Done('ok', 0)
    else if (aConf > oConf) then
      Done(
        Format('Answer: conformed %d, output: conformed %d', [aConf, oConf]), 1)
    else
      Done(
        Format(
          '!!! Answer: conformed %d, output: conformed %d', [aConf, oConf]),
        3
      );
  except
    Halt(2);
  end;
  CloseFile(fi);
  CloseFile(fo);
  CloseFile(fa);
end.