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 <bits/stdc++.h>

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define MAX 101

using namespace std;

int field[MAX][MAX];
bool went[MAX][MAX];
int w, h;
int sx, sy, gx, gy;
int scol;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

bool dfs(int x, int y) {
	if(field[y][x] == 0) return false;
	if(x == gx && y == gy) return true;
	int ncol = field[y][x];
	field[y][x] = 0;
	rep(i, 4) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(0 < nx && nx <= w && 0 < ny && ny <= h && ncol == field[ny][nx]) {
			/*rep(j, h) {
				rep(k, w) cout << field[j][k] << " ";
				cout << endl;
			}*/
			if(dfs(nx, ny)) return true;
		}
	}
	return false;
}

int main() {
	while(cin >> w >> h, w || h) {
		memset(field, 0, sizeof(field));
		memset(went, false, sizeof(went));
		int n;
		cin >> sx >> sy >> gx >> gy;
		cin >> n;
		rep(i, n) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			rep(i, (d ? 4 : 2)) rep(j, (d ? 2 : 4)) field[y + i][x + j] = c;
		}
		/*rep(j, h) {
			rep(k, w) cout << field[j][k] << " ";
			cout << endl;
		}*/
		if(dfs(sx, sy)) cout << "OK" << endl;
		else cout << "NG" << endl;
	}
	return 0;
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

int di[] = {1, 0, -1, 0}, dj[] = {0, 1, 0, -1};

void solve() {
  int w, h;
  while (cin >> w >> h, w) {
    int sx, sy, gx, gy, n;
    cin >> sx >> sy >> gx >> gy >> n;
    sx--, sy--, gx--, gy--;
    vvi v(h, vi(w));
    rep(i, n) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x--;
      y--;
      rep(i, 2 + d * 2) rep(j, 4 - d * 2) v[y + i][x + j] = c;
    }
    int roadColor = v[sy][sx];
    function<bool(int, int)> dfs = [&](int i, int j) {
      if (i < 0 || j < 0 || i >= h || j >= w || v[i][j] != roadColor) return false;
      if (i == gy && j == gx) return true;
      v[i][j] = -1;
      bool b  = false;
      rep(k, 4) b |= dfs(i + di[k], j + dj[k]);
      return b;
    };
    cout << (dfs(sy, sx) ? "OK" : "NG") << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;
int dx[2][8] = { { 0, 1, 2, 3, 0, 1, 2, 3 }, { 0, 1, 0, 1, 0, 1, 0, 1 } };
int dy[2][8] = { { 0, 0, 0, 0, 1, 1, 1, 1 }, { 0, 0, 1, 1, 2, 2, 3, 3 } };

int dx2[4] = {1, 0, -1, 0};
int dy2[4] = {0, -1, 0, 1};

struct Point
{
	int x, y;
	Point(){};
	Point(int a, int b)
	{
		x = a;
		y = b;
	}
};

void solve()
{
	int w, h;
	while (cin >> w >> h, w || h)
	{
		vector< vector<int> > field(h + 1, vector<int>(w + 1));
		Point start, goal;
		cin >> start.x >> start.y >> goal.x >> goal.y;
		int n;
		cin >> n;
		for (int i = 0; i < n; ++i)
		{
			int c, d;
			Point p;
			cin >> c >> d >> p.x >> p.y;
			for (int j = 0; j < 8; ++j)
			{
				int nextX = p.x + dx[d][j];
				int nextY = p.y + dy[d][j];
				field[nextY][nextX] = c;
			}
		}
		if (field[start.y][start.x] != field[goal.y][goal.x])
		{
			cout << "NG" << endl;
			continue;
		}
		queue<Point> que;
		que.push(start);
		bool isOK = false;
		deque< deque<bool> > isUsed(h + 1, deque<bool>(w + 1));
		isUsed[start.y][start.x] = true;
		while (!que.empty())
		{
			Point p = que.front();
			que.pop();
			if (p.x == goal.x && p.y == goal.y)
			{
				isOK = true;
				break;
			}
			for (int i = 0; i < 4; ++i)
			{
				int nx = p.x + dx2[i];
				int ny = p.y + dy2[i];
				
				if ((1 <= nx && nx <= w) && (1 <= ny && ny <= h) && field[ny][nx] == field[start.y][start.x] && isUsed[ny][nx] == false)
				{
					isUsed[ny][nx] = true;
					que.push(Point(nx, ny));
				}
			}
		}
		if (isOK)
		{
			cout << "OK" << endl;
		}
		else
		{
			cout << "NG" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}