Go: package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

var (
	dx = []int{0, -1, 0, 1}
	dy = []int{1, 0, -1, 0}
)

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		w, _ := strconv.Atoi(buf[0])
		h, _ := strconv.Atoi(buf[1])
		if w == 0 && h == 0 {
			break
		}
		scanner.Scan()
		buf_ := strings.Split(scanner.Text(), " ")
		xs, _ := strconv.Atoi(buf_[0])
		ys, _ := strconv.Atoi(buf_[1])
		scanner.Scan()
		buf__ := strings.Split(scanner.Text(), " ")
		xg, _ := strconv.Atoi(buf__[0])
		yg, _ := strconv.Atoi(buf__[1])
		xs--
		ys--
		xg--
		yg--
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		field := make([][]int, 100)
		for i := range  field {
			field[i] = make([]int, 100)
		}
		visited := make([][]bool, 100)
		for i := range visited {
			visited[i] = make([]bool, 100)
		}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf___ := strings.Split(scanner.Text(), " ")
			c, _ := strconv.Atoi(buf___[0])
			d, _ := strconv.Atoi(buf___[1])
			x, _ := strconv.Atoi(buf___[2])
			y, _ := strconv.Atoi(buf___[3])
			x--
			y--
			switch d {
			case 0:
				for j := 0; j < 4; j++ {
					for k := 0; k < 2; k++ {
						field[x+j][y+k] = c
					}
				}
			case 1:
				for j := 0; j < 2; j++ {
					for k := 0; k < 4; k++ {
						field[x+j][y+k] = c
					}
				}
			}
		}
		if field[xs][ys] != field[xg][yg] || // スタートとゴールの色が等しくないと"NG"
			field[xs][ys] == 0 { // スタートの色が無職
			fmt.Println("NG")
			continue
		}
		Q := make([]Entry, 0)
		visited[xs][ys] = true
		Q = append(Q, Entry{xs, ys})
		for len(Q) > 0 {
			row := Q[0].x
			col := Q[0].y
			Q = Q[1:]
			for i := 0; i < 4; i++ {
				if 0 <= row+dx[i] && row+dx[i] < w &&
					0 <= col+dy[i] && col+dy[i] < h && // 移動可能な範囲内かどうか
					!visited[row + dx[i]][col + dy[i]] && // 訪問済みか
					field[row+dx[i]][col+dy[i]]==field[row][col] { // 色が同じかどうか
						visited[row+dx[i]][col+dy[i]] = true
						Q = append(Q, Entry{row+dx[i],col+dy[i]})
				}
			}
		}
		if visited[xg][yg] {
			fmt.Println("OK")
		} else {
			fmt.Println("NG")
		}
	}
}

type Entry struct {
	x, y int
}

C++: //#include <iostream>
//#include <algorithm>
//using namespace std;
// 
//int N;  // ??????????????° (1 <= N <= 100)
//int W;  // ??????????????????????????? (1 <= W <= 10000)
// 
//struct Item {
//    int v;  // ??????????????? (1 <= v <= 1000)
//    int w;  // ??????????????? (1 <= w <= 1000)
//};
//Item items[101];    // ??????????????±
// 
//int C[101][10001];  // ??????????????????????¨???????????????????????¬??????????
///*
//    C[i][w] :
//    ???????????????????????\?????????or??\???????????????i????????????????????§????????????
//    ??????????????????????¨????w????????£???????????????????????§??????????´??????????
//*/
//
//int maxv = 0;  // ?????????????¨?????????§???
// 
//// ??????????????????????????°????????¢??°
//void debug_print(int i, int w)
//{
//    char key_wait;
// 
//    cout << "  C[i][w] = max(C[i-1][w], C[i-1][w - items[i].w] + items[i].v);" << endl;
//    cout << "->C[" << i << "][" << w << "] = max(C[" << i - 1 << "][" << w << "], C[" << i - 1 << "][" << w << " - " << items[i].w << "] + " << items[i].v << ")" << endl;
// 
//    for (int iii = 0; iii <= N; ++iii) {
//        for (int www = 0; www <= W; ++www) {
//            if (C[iii][www] == -1) {
//                cout << " x";
//            }
//            else {
//                cout << " " << C[iii][www];
//            }
//        }
//        cout << endl;
//    }
// 
//    cin >> key_wait;
//}
// 
// 
//int main()
//{
//    int i, w;
// 
//    cin >> N >> W;
// 
//    // ?????? = 0, ?????? = 0 ???????????????????????\???????????????
//    // ?????????????????¨??§?????????????????????????°??????????????????????????????????
//    items[0].v = 0;
//    items[0].w = 0;
//    for (i = 1; i <= N; ++i) {
//        cin >> items[i].v >> items[i].w;
//    }
// 
//    /*
//    ???????????°????????¨????????????-1??§?????????
//    ??????????§£??????????????£??????
//    ????????????????????????????????????????????????
//    */
//    for (i = 0; i <= N; ++i) {
//        for (w = 0; w <= W; ++w) {
//            C[i][w] = -1;
//        }
//    }
// 
//    // ??¢????????????????????\??£???????????´????????????0
//    for (w = 0; w <= W; ++w) {
//        C[0][w] = 0;
//    }
// 
//    for (i = 1; i <= N; ++i) {
//        for (w = 0; w <= W; ++w) {
//            // ?????????????????????i??????????????????????????\???????????´???
//            if (w - items[i].w < 0) {
//                C[i][w] = C[i - 1][w];
//            }
//            // ?????????????????????i????????????????????\??????????£????????????´??????
//            // i????????????????????\???????????£??????????????????
//            // i????????????????????\???????????????w????????£??????????????????????????????
//            // ????????§????????????????´??????????
//            else {
//                C[i][w] = max(C[i - 1][w], C[i - 1][w - items[i].w] + items[i].v);
//            }
//            maxv = max(maxv, C[i][w]);
// 
//            // ???????????°????????¨??¢??°
//            // ???????????????????????¢??????????????°AOJ??§Accepted??????????????????
//            debug_print(i, w);
//        }
//    }
// 
//    cout << maxv << endl;
// 
//    return 0;
//}
/*
#include <iostream>
#include <string>
using namespace std;

#define REP( i, a )     for( int i = 0; i < a; i++ )
#define RREP( i, a )    for( int i = a; i >= 0; i-- )
#define FOR( i, a, b )  for( int i = a; i < b; i++ )
#define RFOR( i, a, b ) for( int i = b; i >= a; i-- )
#define ALL( A ) (A).begin, (A).end()

template <typename T>
void debug( const T& outData ) {
	cout << outData << endl;
}

template <typename T>
void debugAr( const T& outData[], const int& loopNum ) {

	for( int i = 0; i < loopNum; i++ ) cout << outData[i] << endl;

}

int main()
{

	int a;
	string str;

	cin >> a >> str;

	debug( a );
	debug( str );

	return 0;

}
*/

/*#ifndef DEBUG_H
#define DEBUG_H

#include <iostream>

#define REP( i, a )  for( int i = 0; i < a; i++ )
#define RREP( i, a ) for( int i = a; i >= 0; i-- )
#define FOR( i, a, b )  for( int i = a; i < b; i++ )
#define RFOR( i, a, b ) for( int i = b; i >= a; i-- )
#define ALL( A ) (A).begin(), (A).end()

template <typename T>
void prt( T outData )
{

std::cout << outData << endl;

}



#endif


??£????????????
*/

/*
#include <iostream>
#include<algorithm>
using namespace std;

static const int N=100;

int main(){
int i,n,j,p[N+1],m[N+1][N+1],k;
cin>>n;
for(i=1;i<=n;i++){
cin>>p[i-1]>>p[i];
}
for(i=1;i<=n;i++) m[i][i]=0;
for(i=2;i<=n;i++){
for(i=1;i<=n;i++){
int j=i+i-1;
m[i][j]=(i<<21);
for(k=10;k<=j-1;j++){
}
}
}
m[i][j]=min(m[i][j]=

*/
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool N[101][101];
int map[101][101],R,ga,gb,f=0,w,h;
int block(int ,int);
int main()
{
	int i,j,a,n,L,b,c,d,X=0,Y=0,x,y;
	cin>>w>>h;
	while(w!=0||h!=0){
		memset(map,0,sizeof(map));
		memset(N,0,sizeof(N));
		cin>>a>>b;
		cin>>ga>>gb;
		cin>>n;
		for(i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			if(d){
				map[y][x]=c;
				map[y+1][x]=c;
				map[y+2][x]=c;
				map[y+3][x]=c;
				map[y][x+1]=c;
				map[y+1][x+1]=c;
				map[y+2][x+1]=c;
				map[y+3][x+1]=c;

			}
			else{
				map[y][x]=c;
				map[y+1][x+1]=c;
				map[y+1][x+2]=c;
				map[y+1][x+3]=c;
				map[y+1][x]=c;
				map[y][x+1]=c;
				map[y][x+2]=c;
				map[y][x+3]=c;

			}
		}
		R=map[b][a];
		if(block(a,b)) cout<<"OK"<<endl;
		else cout<<"NG"<<endl;
		//for(i=0;i<h;i++){
			//for(j=0;j<w;j++){
			//	cout<<map[i][j];
			//}
				
	//		cout<<endl;
		//}
		cout<<endl;
		f=0;
		cin>>w>>h;
	}
	return 0;
}
int block(int b,int a)
{
	//if(f==1) return 1;
	N[b][a]=1;
	if(ga==a&&gb==b) return 1;
	if(a+1!=w){
		if(map[b][a+1]==R&&N[b][a+1]==0) return block(b,a+1);}
	if(b+1!=h){
		if(map[b+1][a]==R&&N[b+1][a]==0) return block(b+1,a);}
	if(a-1!=-1){
		if(map[b][a-1]==R&&N[b][a-1]==0) return block(b,a-1);}
	if(b-1!=-1){
		if(map[b-1][a]==R&&N[b-1][a]==0) return block(b-1,a);}
	return 0;
}