def dfs(maze, x, y)
  c = maze[y][x]
  return if c == 0

  maze[y][x] = 9

  dfs(maze, x - 1, y) if x > 0 && maze[y][x - 1] == c
  dfs(maze, x, y - 1) if y > 0 && maze[y - 1][x] == c
  dfs(maze, x + 1, y) if maze[y][x + 1] && maze[y][x + 1] == c
  dfs(maze, x, y + 1) if maze[y + 1] && maze[y + 1][x] == c
end

while true
  w, h = gets.split.map(&:to_i)
  break if w == 0 && h == 0

  maze = Array.new(h - 1) {Array.new(w - 1, 0)}

  xs, ys = gets.split.map(&:to_i)
  xg, yg = gets.split.map(&:to_i)

  n = gets.to_i
  n.times do |i|
    c, d, x, y = gets.split.map(&:to_i)
    if d == 0
      xe = x + 3; ye = y + 1
    else
      xe = x + 1; ye = y + 3
    end
    x.upto(xe) do |xt|
      y.upto(ye) do |yt|
        maze[yt - 1][xt - 1] = c
      end
    end
  end

#  dfs(maze, xs - 1, ys - 1)
#  puts (maze[yg - 1][xg - 1] == 9) ? 'OK' : 'NG'
end