while true
    w, h = gets.chomp.split.map(&:to_i)
    break if [w, h] == [0, 0]
    start = gets.chomp.split.map{|x| x.to_i - 1}
    goal = gets.chomp.split.map{|x| x.to_i - 1}
    map = Array.new(h){"." * w}
    n = gets.to_i
    n.times{
        c, d, x, y = gets.chomp.split.map(&:to_i)
        addx, addy = 4, 2
        if d == 1
            addx, addy = 2, 4
        end
        for tempy in 0...addy
            map[tempy + y - 1][x - 1, addx] = c.to_s * addx
        end
    }

    result = "NG"
    color = map[start[1]][start[0]]
    if color != "."
        list = [start]
        until list.empty?
            px, py = list.pop
            map[py][px] = "0"
            if [px, py] == goal
                result = "OK"
                break
            end
            list << [px + 1, py] if px < w - 1 && map[py][px + 1] == color
            list << [px - 1, py] if px >     0 && map[py][px - 1] == color
            list << [px, py + 1] if py < h - 1 && map[py + 1][px] == color
            list << [px, py - 1] if py >     0 && map[py - 1][px] == color
        end
    end
    puts result
end
