#include<stdio.h>
#define N 100
#define M 500

void search(int map[100][100],int x,int y,int c){
  if(map[x][y]==0 || c==0)  return;
  if(map[x][y]!=c && map[x][y]!=9)  return;
  if(map[x][y]==9){
    map[x][y]=7;
    return;
  } 
  map[x][y]=7;
  search(map,x-1,y,c);
  search(map,x+1,y,c);
  search(map,x,y-1,c);
  search(map,x,y+1,c);
}

int main(void){
  int i,num,j,k,l;
  int w[N],h[N];
  int xs[N],ys[N];
  int xg[N],yg[N];
  int n[N];
  int c[N][M],d[N][M],x[N][M],y[N][M];
  int map[N][N];

  for(i=0;;i++){
    scanf("%d%d",&w[i],&h[i]);
    if(w[i]==0 && h[i]==0)  break;
    scanf("%d%d",&xs[i],&ys[i]);
    scanf("%d%d",&xg[i],&yg[i]);
    scanf("%d",&n[i]);
    for(j=0;j<n[i];j++){
      scanf("%d%d%d%d",&c[i][j],&d[i][j],&x[i][j],&y[i][j]);
    }
  }
  num=i;
  for(i=0;i<num;i++){
    for(j=0;j<w[i];j++){
      for(k=0;k<h[i];k++){
        map[j][k]=0;
      }
    }
    for(j=0;j<n[i];j++){
      if(d[i][j]==0){
        for(k=0;k<2;k++)
          for(l=0;l<4;l++)
            map[x[i][j]+l][y[i][j]+k]=c[i][j];
      }else if(d[i][j]==1){
        for(k=0;k<4;k++)
          for(l=0;l<2;l++)
            map[x[i][j]+l][y[i][j]+k]=c[i][j];
      }
    }
    map[xg[i]][yg[i]]=9;
    search(map,xs[i],ys[i],map[xs[i]][ys[i]]);
    if(map[xg[i]][yg[i]]==7)        printf("OK\n");
    else if(map[xg[i]][yg[i]]==9)   printf("NG\n");
  }
  return 0;
}