program check_h;

uses
  testlib{, symbols};

var
  i, j, k, l, m, n, p, sa, sb: longint;
  a, b: array [0..50, 0..50] of longint;

begin
  p := inf.readlongint;
  n := inf.readlongint;
  m := inf.readlongint;

  for i := 1 to n do
    for j := 1 to m do
      a[i][j] := inf.readlongint;

  for i := 1 to n do
    for j := 1 to m do
    begin
      b[i][j] := ouf.readlongint;
      if (i = 1) and (j = 1) and (b[i][j] = -1) and (ouf.seekeof) then
        quit(_wa, '-1');
      if abs(b[i][j]) > 1000 + p then
        quit(_wa, 'surely invalid number: ');
      if abs(a[i][j] - b[i][j]) > p then
        quit(_wa, 'matrix B differs from matrix A by more then p: ' +
        'a[][] =  ' +
        'b[][] = ');
      if b[i][j] mod p <> 0 then
        quit(_wa, 'B is not divisible by , it contains ');
    end;

  for i := 1 to n do
  begin
    sa := 0;
    sb := 0;
    for j := 1 to m do
    begin
      sa := sa + a[i][j];
      sb := sb + b[i][j];
    end;
    if sa <> sb then
      quit(_wa, 'profiles differ: row  A sum is , B sum is ');
  end;

  for j := 1 to m do
  begin
    sa := 0;
    sb := 0;
    for i := 1 to n do
    begin
      sa := sa + a[i][j];
      sb := sb + b[i][j];
    end;
    if sa <> sb then
      quit(_wa, 'profiles differ: column  A sum is , B sum is ');
  end;

  quit(_ok, '');
end.
