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++: // 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>
using namespace std;

int block[100][100];
int xs, ys, xg, yg, n, w, h;

bool dfs(int x, int y, int c)
{
  if(block[x][y] != c || block[x][y] == 0)
    return false;
  if(x == xg && y == yg)
    return true;
  bool b = false;
  block[x][y] = 0;
  if(x < w - 1)
    b |= dfs(x + 1, y, c);
  if(x > 0 && !b)
    b |= dfs(x - 1, y, c);
  if(y < h - 1 && !b)
    b |= dfs(x, y + 1, c);
  if(y > 0 && !b)
    b |= dfs(x, y - 1, c);
  block[x][y] = c;
  return b;
}

int main()
{
  while(true)
  {
    cin >> w >> h;
    if(!w && !h)
      break;
    cin >> xs >> ys
      >> xg >> yg >> n;
    xs--; ys--;
    xg--; yg--;
    
    for(int i = 0; i < n; i++)
    {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x--; y--;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
      if(d == 0)
        x += 2;
      else
        y += 2;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
    }
    if(dfs(xs, ys, block[xs][ys]))
      cout << "OK\n";
    else
      cout << "NG\n";
  }
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

const int dir[][2] = {
	1, 0,
	0, 1,
	-1, 0,
	0, -1
};

string solve( VVI board, int sy, int sx, int gx, int gy )
{
	const int H = board.size(), W = board[0].size();

	int color = board[ sy ][ sx ];
	
	queue< PII > que;
	que.push( MP( sy, sx ) );

	set< PII > visited;
	visited.insert( MP( sy, sx ) );

	while ( !que.empty() )
	{
		auto cur = que.front();
		que.pop();

		if ( cur == MP( gy, gx ) )
		{
			return "OK";
		}

		REP( d, 0, 4 )
		{
			auto next = cur;

			next.fst += dir[d][0];
			next.snd += dir[d][1];

			if ( !( 0 <= next.fst && next.fst < H && 0 <= next.snd && next.snd < W ) || 
				 EXIST( visited, next ) ||
				 board[ next.fst ][ next.snd ] != color )
			{
				continue;
			}

			que.push( next );
			visited.insert( next );
		}
	}

	return "NG";
}			

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	while ( true )
	{
		int w, h;
		cin >> w >> h;

		if ( !( w | h ) )
		{
			break;
		}

		int sx, sy, gx, gy, n;
		cin >> sx >> sy >> gx >> gy >> n;

		sx--;
		sy--;
		gx--;
		gy--;

		VVI board( h, VI( w, 0 ) );

		REP( i, 0, n )
		{
			int c, d, x, y;
			cin >> c >> d >> x >> y;

			x--;
			y--;

			if ( !d )
			{
				REP( j, 0, 2 )
				{
					REP( k, 0, 4 )
					{
						board[ y + j ][ x + k ] = c;
					}
				}
			}
			else
			{
				REP( j, 0, 4 )
				{
					REP( k, 0, 2 )
					{
						board[ y + j ][ x + k ] = c;
					}
				}
			}
		}

		cout << solve( board, sy, sx, gy, gx ) << endl;
	}

	return 0;
}