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
}

Python: """ Created by Jieyi on 9/20/16. """


def block(board, w, h, direction, color):
    # horizontal.
    if direction == 0:
        for i in range(2):
            for j in range(4):
                board[h + i][w + j] = color
    # vertical.
    elif direction == 1:
        for i in range(2):
            for j in range(4):
                board[h + j][w + i] = color


def print_board(board):
    for i in range(len(board)):
        print(board[i])


def go_maze(board, s_w, s_h, g_w, g_h):
    place = board[s_h][s_w]
    if place == 0:
        return 'NG'
    w, h = s_w, s_h
    stack = []

    while True:
        board[h][w] = place + 1
        if h + 1 < len(board) and board[h + 1][w] == place:
            h += 1
            stack.append((w, h))
        elif w + 1 < len(board[h]) and board[h][w + 1] == place:
            w += 1
            stack.append((w, h))
        elif h - 1 > 0 and board[h - 1][w] == place:
            h -= 1
            stack.append((w, h))
        elif w - 1 > 0 and board[h][w - 1] == place:
            w -= 1
            stack.append((w, h))
        else:
            if len(stack) == 0 and board[h + 1][w] != place and board[h][w + 1] != place and board[h - 1][w] != place and board[h][w - 1] != place:
                return 'NG'
            (w, h) = stack.pop()

        if w == g_w and h == g_h:
            return 'OK'


def main():
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break

        board = [[0 for _ in range(110)] for _ in range(110)]
        s_w, s_h = map(int, input().split())
        g_w, g_h = map(int, input().split())
        for _ in range(int(input())):
            color, direction, w, h = map(int, input().split())
            block(board, w, h, direction, color)

        # print_board(board)
        print(go_maze(board, s_w, s_h, g_w, g_h))


if __name__ == '__main__':
    main()

Python: while True:
    w,h=map(int, input().split())
    if w==h==0:
        break
    xs,ys=map(int, input().split())
    xg,yg=map(int, input().split())
    xs-=1
    ys-=1
    xg-=1
    yg-=1
    n=int(input())
    dataset=[]
    for _ in range(n):
        dataset.append(list(map(int,input().split())))

    maze=[ [0 for i in range(w) ] for j in range(h)]

    for data in dataset:
        color=data[0]
        drc=data[1]
        x=data[2]-1
        y=data[3]-1
        x_list=[x,x+1] if drc==1 else [x,x+1,x+2,x+3]   
        y_list=[y,y+1,y+2,y+3] if drc==1 else [y,y+1]

        for dy in y_list:
            for dx in x_list:
                maze[dy][dx]=color

    history=[[-1 for i in range(w)] for j in range(h)]
    ws=[]
    dx_list=[0,0,1,-1]
    dy_list=[1,-1,0,0]

    flag=False

    if maze[ys][xs]!=0:
        ws.append([xs,ys])

    while len(ws)!=0:
        current=ws.pop(0)
        current_color=maze[current[1]][current[0]]
        if current[0]==xg and current[1]==yg:
            flag=True 
            break
        for dx,dy in zip(dx_list,dy_list):
            next_x=current[0]+dx
            next_y=current[1]+dy
            if next_x>=0 and next_x<w and next_y>=0 and next_y<h and maze[next_y][next_x]==current_color and history[next_y][next_x]==-1:
                history[next_y][next_x]=0
                ws.append([next_x,next_y])

    if flag:
        print('OK')
    else:
        print('NG')

Python: from sys import stdin
readline = stdin.readline


from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
Block = namedtuple('Block', ['color', 'direction', 'x', 'y'])


def occupation_point(block):
    x, y = block.x, block.y
    d = [(0, 0), (1, 0), (0, 1), (1, 1)]
    for dx, dy in d:
        yield x + dx, y + dy
    if block.direction:
        y += 2
    else:
        x += 2
    for dx, dy in d:
        yield x + dx, y + dy

def paintout(board, start, value):
    color = board[start.y][start.x]
    if color == 0:
        return
    que =[(start.x, start.y)]
    while que:
        x,y = que.pop()
        if board[y][x] == color:
            board[y][x] = value
            que.extend([(x + dx, y + dy) for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]])


def is_reachable(size, start, goal, blocks):
    board = [[0] * (size.x + 2) for _ in range(size.y + 2)]
    for bi in blocks:
        for x, y in occupation_point(bi):
            board[y][x] = bi.color
    paintout(board, start, -1)
    return board[goal.y][goal.x] == -1


while True:
    size = Point(*map(int, readline().split()))
    if size.x == 0:
        break
    start = Point(*map(int, readline().split()))
    goal = Point(*map(int, readline().split()))
    blocks = []
    for _ in range(int(readline())):
        blocks.append(Block(*map(int, readline().split())))
    print('OK' if is_reachable(size, start, goal, blocks) else 'NG')