import java.util.*

fun main(args:Array<String>?): Unit {
    val neighbors = listOf(Point(1, 0), Point(-1, 0), Point(0, 1), Point(0, -1))
    while(true){
        val (w, h) = readLine()!!.trim().split(' ').map(String::toInt)
        if (w == 0 && h == 0) return
        val board = Array(h){IntArray(w){0}}
        val (sx, sy) = readLine()!!.trim().split(' ').map(String::toInt)
        val (gx, gy) = readLine()!!.trim().split(' ').map(String::toInt)
        val start = Point(sx - 1, sy - 1)
        val goal = Point(gx - 1, gy - 1)
        val n = readLine()!!.trim().toInt()
        repeat(n){
            val (c, d, x, y) = readLine()!!.trim().split(' ').map(String::toInt)
            for (cell in Block(Point(x - 1, y - 1), d).cells()) {
                board[cell.y][cell.x] = c
            }
        }
        val queue: Queue<Point> = LinkedList()
        if (board[start.y][start.x] != 0) queue.add(start)
        while(queue.isNotEmpty() && queue.peek() != goal){
            val current = queue.poll()
            for (neighbor in neighbors.map{it + current}) if (neighbor.y in 0 until h && neighbor.x in 0 until w && board[neighbor.y][neighbor.x] != 0){
                if (board[current.y][current.x] == board[neighbor.y][neighbor.x]){
                    queue.add(neighbor)
                }
            }
            board[current.y][current.x] = 0
        }
        println(
                if (queue.isNotEmpty()) "OK" else "NG"
        )
    }
}
data class Point(val x: Int, val y: Int) {
    operator fun plus(that: Point): Point = Point(x + that.x, y + that.y)
}
data class Block(val position: Point, val state: Int) {
    fun cells(): Iterable<Point> {
        return when(state){
            0 -> listOf(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 0),
                    Point(0, 1), Point(1, 1), Point(2, 1), Point(3, 1)).map{it + position}
            else -> listOf(Point(0, 0), Point(0, 1), Point(0, 2), Point(0, 3),
                    Point(1, 0), Point(1, 1), Point(1, 2), Point(1, 3)).map{it + position}
        }
    }
}
