{ VERIFICATION PROGRAM for TEAMS problem for NEERC'2001 }
{ (C) Roman Elizarov }
{$APPTYPE CONSOLE}
program TEAMS_CHECK;
uses
  sysutils, testlib in '..\testlib.pas';

const 
  MAXN = 100;

var
  n, i, j: integer;
  a: array[1..MAXN, 1..MAXN] of boolean;

type
  TTeam = set of 1..MAXN;

  TAnswer = class
  public 
    NoAnswer: boolean;
    SizeDiff: integer;

    constructor Create(const st: InStream);
  end;

var
  sans, souf: TAnswer;

function ReadTeam(const st: InStream; s: string; out g: TTeam): integer;
var
  ss: string;
  code: integer;
  num: integer;
  first: boolean;
  cnt: integer;
begin
  Result := -1;
  cnt := -1;
  g := [];
  s := Trim(s);
  first := true;
  if s = '' then
    st.Quit(_PE, 'Empty line instead of a team');
  while s <> '' do begin
    i := Pos(' ', s);
    if i = 0 then begin
      ss := s;
      s := '';
    end else begin
      ss := copy(s, 1, i - 1);
      s := copy(s, i + 1, length(s) - i);
    end;
    val(ss, num, code);
    if code <> 0 then
      st.Quit(_PE, 'Not a number: ' + ss);
    if first then begin
      cnt := num;
      if (cnt < 1) or (cnt >= n) then
        st.Quit(_PE, 'Wrong number of persons in a team: ' + ss);
      first := false;
      Result := cnt;
    end else begin
      dec(cnt);
      if cnt < 0 then
        st.Quit(_PE, 'Too many numbers in team desc: ' + ss);
      if (num < 1) or (num > n) then
        st.Quit(_PE, 'Invalid person: ' + ss);
      if num in g then 
        st.Quit(_PE, 'Person occurs twice in a team: ' + ss);
      Include(g, num);
    end;
  end;
  if cnt <> 0 then
    st.Quit(_PE, 'Too few persons in a team line');
end;

procedure CheckTeam(const st: InStream; const g: TTeam);
var
  i, j: integer;
begin
  for i := 1 to n do 
    if i in g then
      for j := 1 to n do
        if (j <> i) and (j in g) then
          if not a[i, j] then
            st.Quit(_WA, IntToStr(i) + ' and ' + IntToStr(j) + ' are in the same team but do not know each other');
end;

constructor TAnswer.Create(const st: InStream); 
var
  s: string;
  g1cnt, g2cnt: integer;
  g1, g2: TTeam;
begin
  s := st.readString;
  NoAnswer := Trim(s) = 'No solution';
  if NoAnswer then begin
    if not st.seekeof then
      st.Quit(_PE, 'Extra data in file');
    exit;
  end;
  g1cnt := ReadTeam(st, s, g1);
  g2cnt := ReadTeam(st, st.readString, g2);
  if not st.seekeof then
    st.Quit(_PE, 'Extra data in file');
  if g1 * g2 <> [] then
    st.Quit(_WA, 'Teams intersect');
  if g1 + g2 <> [1..n] then
    st.Quit(_WA, 'Not everyone is in a team');
  CheckTeam(st, g1);
  CheckTeam(st, g2);
  SizeDiff := abs(g1cnt - g2cnt);
end;


begin
  try 
    { Read input }
    n := inf.readInteger;
    if (n < 2) or (n > MAXN) then
      Quit(_FAIL, 'Invalid N=' + IntToStr(n));
    fillchar(a, sizeof(a), 0);
    for i := 1 to n do begin
      a[i, i] := true;
      while true do begin
        j := inf.readInteger;
        if j = 0 then
          break;
        a[i, j] := true;
      end;
    end;
    { Read answer }
    sans := TAnswer.Create(ans);
    { Read output }
    souf := TAnswer.Create(ouf);
    { Compare them }
    if souf.NoAnswer then begin
      if sans.NoAnswer then
        Quit(_OK, 'Ok')
      else
        Quit(_WA, 'Solution is not found where it exists');
    end else begin
      if sans.NoAnswer then
        Quit(_FAIL, 'Solution was not found by jury, but by contestant!!!')
      else if souf.SizeDiff = sans.SizeDiff then
        Quit(_OK, 'Ok')
      else if souf.SizeDiff > sans.SizeDiff then
        Quit(_WA, 'Found worse solution with diff ' +
            IntToStr(souf.SizeDiff) + ' instead of ' + IntToStr(sans.SizeDiff))
      else
        Quit(_FAIL, 'Solution by contestant is better than by jury!!! Diff is ' +
            IntToStr(souf.SizeDiff) + ' instead of ' + IntToStr(sans.SizeDiff));
    end;
  except on e: Exception do 
    Quit(_FAIL, e.Message);
  end;
end.
