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++: # define _CRT_SECURE_NO_WARNINGS 1
# define _USE_MATH_DEFINES
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <unordered_set>
# include <unordered_map>
# include <climits>
# include <complex>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;
# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define FORI(i,k,n) for(int i=k;i<(int)n;i++)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define STR(x) string x;cin>>x;
# define ALL(x) begin(x),end(x)
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<":"<<x<<endl;
# define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
//const int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }



int main()
{
	while (true)
	{
		int w, h;
		pii start, goal;
		int n;
		int a[200][200] = {};
		bool flag[200][200] = {};
		bool ok = false;
		cin >> w >> h;if (!w)break;
		cin >> start.first >> start.second;
		cin >> goal.first >> goal.second;
		cin >> n;

		FOR(i, n)
		{
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			for (int X = x; X <= x+1+(d?0:2); X++)
			{
				for (int Y = y; Y <= y + 1 + (d ? 2 : 0); Y++)
				{
					a[X][Y] = c;
				}
			}
		}

		flag[start.first][start.second] = true;
		queue<pii> que; 
		if(a[start.first][start.second]!=0)que.push(start);

		while (!que.empty())
		{
			pii now = que.front(); que.pop();
			if (now == goal)
			{
				cout << "OK" << endl;
				ok = true;
			}

			FOR(i, 4)
			{
				pii next=now;
				next.first += dx[i]; next.second += dy[i];

				if (a[next.first][next.second] == a[now.first][now.second] && !flag[next.first][next.second])
				{
					flag[next.first][next.second] = true;
					que.push(next);
				}
			}
		}

		if (!ok)cout << "NG" << endl;
	}
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<climits>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reg(i,a,b) for(int i=(a);i<=(b);i++)
#define irep(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define ireg(i,a,b) for(int i=(b);i>=(a);i--)
typedef long long int lli;
typedef pair<int,int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX


int map[150][150]={};
int gone[150][150]={};

int gx,gy;
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};

bool dfs(int nx,int ny){
	if(gone[nx][ny])return false;
	gone[nx][ny]=1;
	if(nx == gx && ny == gy)return true;
	bool res = false;
	rep(i,4){
		int tx = nx+dx[i],ty=ny+dy[i];
		if(map[tx][ty]!=map[nx][ny])continue;
		res |= dfs(tx,ty);
	}
	return res;
}

int main(void){
	while(1){
		int w,h;
		scanf("%d%d",&w,&h);
		if(w==0 && h==0)break;
		int sx,sy,n;
		scanf("%d%d%d%d%d",&sx,&sy,&gx,&gy,&n);
		memset(map,0,sizeof(map));
		memset(gone,0,sizeof(gone));
		rep(i,n){
			int c,d,nx,ny;
			scanf("%d%d%d%d",&c,&d,&nx,&ny);
			int fx=2,fy=4;
			if(d==0)swap(fx,fy);
			rep(x,fx)rep(y,fy){
				map[x+nx][y+ny]=c;
			}
		}
		/*
		rep(y,20)rep(x,20){
			printf("%d%c",map[x][y],x==19?'\n':' ');
		}*/
		if(map[sx][sy]!=0 && dfs(sx,sy))printf("OK\n");
		else printf("NG\n");
	}
		
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int main() {
	int w, h, sx, sy, gx, gy, n;
	while (cin >> w >> h, w) {
		cin >> sx >> sy >> gx >> gy >> n;
		int map[110][110] = {};
		for (int i = 0; i < n; i++) {
			int c, d, x, y; cin >> c >> d >> x >> y;
			map[x][y] = c;
			map[x][y + 1] = c;
			map[x + 1][y] = c;
			map[x + 1][y + 1] = c;
			if (d == 0) {
				map[x + 2][y] = c;
				map[x + 2][y + 1] = c;
				map[x + 3][y] = c;
				map[x + 3][y + 1] = c;
			}
			else {
				map[x][y + 2] = c;
				map[x + 1][y + 2] = c;
				map[x][y + 3] = c;
				map[x + 1][y + 3] = c;
			}
		}
		queue<pair<int, int> >q;
		int p = map[sx][sy];
		if (p == 0)goto museum;
		q.push(make_pair(sx, sy));
		while (q.size()) {
			int x = q.front().first, y = q.front().second;
			q.pop();
			map[x][y] = 0;
			if (x == gx&&y == gy)goto heaven;
			if (map[x][y - 1] == p)q.push(make_pair(x, y - 1));
			if (map[x][y + 1] == p)q.push(make_pair(x, y + 1));
			if (map[x - 1][y] == p)q.push(make_pair(x - 1, y));
			if (map[x + 1][y] == p)q.push(make_pair(x + 1, y));
		}
		museum:cout << "NG\n"; goto park;
	heaven:cout << "OK\n";
	park:;
	}
}