#include <iostream>
using namespace std;

int F[102][102];

bool DFS(int Y, int X, int C, int yg, int xg)
{
  if(Y == yg && X == xg)
    return true;
  if(F[Y][X]!=C)
    return false;

  F[Y][X] = 0;

  bool tmp = 0;
  tmp += DFS(Y-1, X, C, yg, xg);
  tmp += DFS(Y+1, X, C, yg, xg);
  tmp += DFS(Y, X-1, C, yg, xg);
  tmp += DFS(Y, X+1, C, yg, xg);
  return tmp;
}

int main()
{
  while(1){
    F[102][102] = {};

    int w, h;
    cin >> w >> h;

    if(w == 0 && h == 0)
      return 0;

    int xs, ys;
    cin >> xs >> ys;

    int xg, yg;
    cin >> xg >> yg;

    int n;
    cin >> n;

    int c, d, x, y;
    for(int i=0; i<n; i++){
      cin >> c >> d >> x >> y;
      if(d == 0)
        for(int j=0; j<2; j++)
          for(int k=0; k<4; k++)
            F[y+j][x+k] = c;
      else
        for(int j=0; j<4; j++)
          for(int k=0; k<2; k++)
            F[y+j][x+k] = c;
    }

    bool res = DFS(ys, xs, F[ys][xs], yg, xg);

    if(res)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}

