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

int F[102][102];

void DFS(int Y,int X,int C){
   if(F[Y][X]!=C)
      return;
   F[Y][X]=6;
   DFS(Y-1,X,C);
   DFS(Y,X+1,C);
   DFS(Y+1,X,C);
   DFS(Y,X-1,C);
} 

int main(){
   int w,h,xs,ys,xg,yg,n;
   int c,d,x,y;
   while(true){
      cin >> w >> h;
      if(w==0&&h==0)
         break;
      cin >> xs >> ys;
      cin >> xg >> yg;
      cin >> n;
      for(int i=1;i<=n;i++){
         cin >> c >> d >> x >> y;
         if(d==0){
             F[y+1][x+1]=c;
             F[y+1][x+2]=c;
             F[y+1][x+3]=c;
             F[y+1][x+4]=c;  
             F[y+2][x+1]=c;
             F[y+2][x+2]=c;
             F[y+2][x+3]=c;
             F[y+2][x+4]=c;   
         }
         else{
             F[y+1][x+1]=c;
             F[y+2][x+1]=c;
             F[y+3][x+1]=c;
             F[y+4][x+1]=c;  
             F[y+1][x+2]=c;
             F[y+2][x+2]=c;
             F[y+3][x+2]=c;
             F[y+4][x+2]=c;   
         }
      }
      if(F[ys+1][xs+1]!=1&&F[ys+1][xs+1]!=2&&F[ys+1][xs+1]!=3&&F[ys+1][xs+1]!=4&&F[ys+1][xs+1]!=5)
         cout << "NG" << endl;
      else{
         DFS(ys+1,xs+1,c);
         if(F[yg+1][xg+1]==F[ys+1][xs+1])
            cout << "OK" << endl;
         else
            cout << "NG" << endl;
         for(int i=1;i<=w;i++)
            for(int j=1;j<=h;j++)
               F[i][j]=0;
      }
   }
   return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <cassert>
#include <utility>

using namespace std;

typedef pair<int, int> P;

#define mp(a,b) make_pair(a,b)
#define DEBUG(x) cerr<<#x<<":"<<x<<endl;

int W, H;
int SX, SY, GX, GY;

int field[100][100];

inline bool inRange(int x, int y) {
	return 0 <= x && x < W && 0 <= y && y < H;
}

void setBlock(int c, int d, int x, int y) {
	if(d == 0) {
		for(int dy = 0; dy < 2; dy++) {
			for(int dx = 0; dx < 4; dx++) {
				field[x + dx][y + dy] = c;
			}
		}
	}
	else if(d == 1) {
		for(int dy = 0; dy < 4; dy++) {
			for(int dx = 0; dx < 2; dx++) {
				field[x + dx][y + dy] = c;
			}
		}
	}
	else
		assert(false);
}

void pv() {
	cerr << endl;
	cerr << "-----" << endl;
	for(int y = 0; y < H; y++) {
		for(int x = 0; x < H; x++)
			cerr << field[x][y];
		cerr << endl;
	}
	cerr << "-----" << endl;
}

bool visit[100][100];

int main() {
	for(; cin >> W >> H;) {
		cin >> SX >> SY >> GX >> GY;
		SX--, SY--, GX--, GY--;

		memset(field, 0, sizeof field);
		memset(visit, 0, sizeof visit);
		
		int N; cin >> N;
		for(int i = 0; i < N; i++) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			x--, y--;
			setBlock(c, d, x, y);
		}

		queue<P> Q;
		Q.push(mp(SX, SY));
		visit[SX][SY] = true;

		int C = field[SX][SY];

		bool f = false;
		while(!Q.empty()) {
			P p = Q.front(); Q.pop();
			if(p.first == GX && p.second == GY) {
				f = true;
				break;
			}
			for(int i = 0; i < 4; i++) {
				int dx[4] = {1, 0, -1, 0};
				int dy[4] = {0, 1, 0, -1};
				int nx = p.first + dx[i];
				int ny = p.second + dy[i];
				if(inRange(nx, ny) && field[nx][ny] == C && visit[nx][ny] == false) {
					visit[nx][ny] = true;
					Q.push(mp(nx, ny));
				}
			}
		}

		cout << (f ? "OK" : "NG") << endl;
	}
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <queue>
#include <set>
using namespace std;

#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c){T n=0;bool m=false;while(isspace(c)){c=gcu();}if(c=='-')m=true,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m?-n:n;}
_in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) {pcu('0'+b);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_vo(string s){for(char c:s)pcu(c);}
#endif
_T _vo(T n){static char buf[20];char *p=buf;
	if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10;
	while (p!=buf)pcu(*--p);}
_vl(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _vo(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}

using V = vector<int>;
using VV = vector<V>;
using P = pair<int, int>;

int main() {
	int dx[] = {4, 2}, dy[] = {2, 4};
	for (int w, h; w = in(), h = in();) {
		P s = {in() - 1, in() - 1}, g = {in() - 1, in() - 1};
		int n = in();
		VV m(h, V(w));
		while (n--) {
			int c = in(), d = in(), x = in() - 1, y = in() - 1;
			for (int i = 0; i < dy[d]; i++)
				for (int j = 0; j < dx[d]; j++)
					m[i + y][j + x] = c;
		}
		auto f = [&]{
			queue<P> q;
			q.push(s);
			int c = m[s.second][s.first];
			set<P> v;
			v.insert(s);
			while (!q.empty()) {
				P t = q.front();
				q.pop();
				if (t == g)
					return "OK";
				P d = {1, 0};
				for (int i = 0; i < 4; i++) {
					P u = {t.first + d.first, t.second + d.second};
					if (u.first >= 0 && u.second >= 0 &&
						u.first < w && u.second < h &&
						m[u.second][u.first] == c && !v.count(u)) {
						v.insert(u);
						q.push(u);
					}
					swap(d.first, d.second *= -1);
				}
			}
			return "NG";
		};
		outl(f());
	}
}