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<stdio.h>
int w,h;
int is(int x,int y){
	return (x>=0 && x <= w && y>=0 && y<= h);
}
int main(void){
	const int dx[4]={0,1,0,-1};
	const int dy[4]={1,0,-1,0};
	int qx[100000];
	int qy[100000];
	int p,mx;
    int n,m[101][101]={0};
    int xs,ys,xg,yg,a,c,d,x,y,cnt,f;
	for(;;){
		scanf("%d %d",&w,&h);
		if(w==0)break;
		for(int i=0;i<=w;i++){
			for(int j=0;j<=h;j++){
				m[i][j]=0;
			}
		}
		scanf("%d %d %d %d",&xs,&ys,&xg,&yg);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			for(int j=0;j<(d==0?2:4);j++){
				for(int k=0;k<(d==0?4:2);k++){
					m[x+k][y+j]=c;
				}
			}
		}
		c = m[xs][ys];
		p=0;
		mx=1;
		qx[0]=xs;
		qy[0]=ys;
		f=1;
/*
		for(int i=0;i<=h;i++){
			for(int j=0;j<=w;j++){
				printf("%d ",m[j][i]);
			}
			puts("");
		}
		printf("%d %d\n",c,m[xg][yg]);*/
		while(f && p<mx){
			cnt = 0;
			for(int i=0;i<4;i++){
				x = qx[p] + dy[i];
				y = qy[p] + dx[i];
				if(is(x,y) && m[x][y] == c){
					m[x][y]=-1;
					cnt++;
					qx[p+cnt] = x;
					qy[p+cnt] = y;
					if(x == xg && y == yg)f=0;
				}
			}
			mx += cnt;
			p++;
		}
		puts(f?"NG":"OK");
	}
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)

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

const int ax[2][8] = {
	{0, 1, 2, 3, 0, 1, 2, 3},
	{0, 0, 0, 0, 1, 1, 1, 1}
};

const int ay[2][8] = {
	{0, 0, 0, 0, 1, 1, 1, 1},
	{0, 1, 2, 3, 0, 1, 2, 3}
};

int W, H, sx, sy, gx, gy, n, board[110][110];

bool dfs(int x, int y) {
	if (x == gx && y == gy) return true;
	int c = board[y][x];
	board[y][x] = -1;
	
	for_(d,0,4) {
		int nx = x + dx[d], ny = y + dy[d];
		if (nx < 1 || W <= nx || ny < 1 || H <= ny) continue;
		if (board[ny][nx] != c) continue;
		if (dfs(nx, ny)) return true;
	}
	
	return false;
}

int main() {
	while (cin >> W >> H, W) {
		memset(board, -1, sizeof(board));
		
		cin >> sx >> sy;
		cin >> gx >> gy;
		cin >> n;
		
		for_(i,0,n) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			for_(a,0,8) board[y + ay[d][a]][x + ax[d][a]] = c;
		}
		
		if (board[sy][sx] != -1 && dfs(sx, sy)) puts("OK");
		else puts("NG");
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
  
// #define int long long
#define pb push_back
  
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
 
template <class X> void print(X Target){ cout << Target << '\n'; }

const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int w, h;
int xs, ys, xg, yg;
int mp[128][128];
bool field[128][128];

void dfs(int y, int x, int color)
{
  if (x < 1 || x > w || y < 1 || y > h) return;
  if (field[y][x]) return;
  field[y][x] = true;
  for (int i = 0; i < 4; i++){
    if (mp[y + dy[i]][x + dx[i]] == color){
      dfs(y + dy[i], x + dx[i], color);
    }
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  int n;

  while (cin >> w >> h, w || h){
    memset(field, false, sizeof(field));
    memset(mp, 0, sizeof(mp));
    cin >> xs >> ys >> xg >> yg >> n;
    for (int i = 0; i < n; i++){
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d){
        for (int j = 0; j < 4; j++){
          for (int k = 0; k < 2; k++){
            mp[y + j][x + k] = c;
          }
        }
      }
      else {
        for (int j = 0; j < 2; j++){
          for (int k = 0; k < 4; k++){
            mp[y + j][x + k] = c;
          }
        }
      }
    }
    dfs(ys, xs, mp[ys][xs]);
    (field[yg][xg]) ? print("OK") : print("NG");
  }

  return (0);
}

C++: // 0207
#include <iostream>
#include <vector>
using namespace std;

void maze_fill(vector<vector<int> > &maze, int c, int d, int x, int y){
	int w, h;
	if(d == 0){
		w = 4;
		h = 2;
	}else{
		w = 2;
		h = 4;
	}

	for(int i=y;i<y+h;i++)
		for(int j=x;j<x+w;j++)
			maze[i][j] = c;
}

void search(vector<vector<int> > &maze, int x, int y, int c){
	maze[y][x] = 6;

	if(maze[y-1][x] == c)
		search(maze, x, y-1, c);
	
	if(maze[y][x-1] == c)
		search(maze, x-1, y, c);
	
	if(maze[y+1][x] == c)
		search(maze, x, y+1, c);
	
	if(maze[y][x+1] == c)
		search(maze, x+1, y, c);
}

int main(){
	while(1){
		int w, h, xs, ys, xg, yg, n;
		vector<vector<int> > maze;

		cin>>w>>h;
		if((w == 0) && (h == 0))
			break;

		maze.resize(h+2);
		for(int i=0;i<h+2;i++)
			maze[i].resize(w+2);

		for(int i=0;i<h+2;i++){
			if((i == 0) || (i == h+1)){
				for(int j=0;j<w+2;j++)
					maze[i][j] = 9;
			}else{
				maze[i][0] = 9;
				maze[i][w+1] = 9;
				for(int j=1;j<w+1;j++)
					maze[i][j] = 0;
			}
		}

		cin>>xs>>ys;
		cin>>xg>>yg;
		cin>>n;
		for(int i=0;i<n;i++){
			int c, d, x, y;
			cin>>c>>d>>x>>y;
			maze_fill(maze, c, d, x, y);
		}
		
		search(maze, xs, ys, maze[ys][xs]);

		if(maze[ys][xs] == maze[yg][xg])
			cout<<"OK"<<endl;
		else
			cout<<"NG"<<endl;
	}

	return 0;
}