#include <iostream>
using namespace std;
 
int C[102][102];
int ans;
int xg,yg;
 
void meiro(int W,int H,int c){
  if(W==xg && H==yg){
    ans = 1;
    return;
  }
 
  if(C[W][H]!=c){
    return;
  }
 
  C[W][H] = 0;
 
  meiro(W-1,H  ,c);
  meiro(W  ,H+1,c);
  meiro(W+1,H  ,c);
  meiro(W  ,H-1,c);
}
 
int main(){
  while(1){
    for(int i=0;i<102;i++){
      for(int j=0;j<102;j++){
        C[i][j] = 0;
      }
    }
     
    int w,h;
    cin >> w >> h;
 
    if(w==0 && h==0){
      break;
    }
 
    int xs,ys;
    cin >> xs >> ys;
 
    cin >> xg >> yg;
 
    int n;
    cin >> n;
    int c[n],d[n],x[n],y[n];
 
    int cbase;
 
    for(int i=0;i<n;i++){
      cin >> c[i] >> d[i] >> x[i] >> y[i];
 
      if(d[i]==0){
        for(int j=x[i];j<x[i]+4;j++){
          for(int k=y[i];k<y[i]+2;k++){
            C[j][k]=c[i];
          }
        }
      }
      if(d[i]==1){
        for(int j=x[i];j<x[i]+2;j++){
          for(int k=y[i];k<y[i]+4;k++){
            C[j][k]=c[i];
          }
        }
      }
    }
    ans = 0;
 if(C[xs][ys] == 0){
      cout << "NG" << endl;
      continue;
    }
    meiro(xs,ys,C[xs][ys]);
   
    if(ans==1){
      cout << "OK" << endl;
    }else{
      cout << "NG" << endl;
    }
  }
 
  return 0;
}
