#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  int h, w;
  while (scanf("%d%d", &h, &w) != EOF && h && w) {
    int S = h*h+w*w;
    bool finished = false;
    for (int i=h+1; i<=150; i++) {
      int W = S - i*i;
      for (int j=i+1; j<150; j++) {
	if (j * j == W) {
	  finished = true;
	  printf("%d %d\n", i, j);
	  goto END;
	}
      }
    }
    while (!finished) {
      S++;
      for (int i=1; i<=150; i++) {
	for (int j=i+1; j<150; j++) {
	  if (S == i*i+j*j) {
	    printf("%d %d\n", i, j);
	    goto END;
	  } else if (S < i*i+j*j) {
	    break;
	  }
	}
      }
    }
  END: continue;
  }
}
#include <bits/stdc++.h>

using namespace std;

int mp[101][101];
int w, h;
int gx, gy;

int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};

bool dfs(int y, int x, int c)
{
	if (y < 1 || y > h || x < 1 || x > w || mp[y][x] != c) return false;

	if (y == gy && x == gx) return true;

	mp[y][x] = 0;

	bool res = false;

	for (int i = 0; i < 4; ++i) res = res || dfs(y + dy[i], x + dx[i], c);

	return res;
}

int main()
{
	while (cin >> w >> h, w || h) {
		int sx, sy;
		cin >> sx >> sy >> gx >> gy;
		int n; cin >> n;
		while (n--) {
			int c, d, x, y; cin >> c >> d >> x >> y;
			if (d) {
				for (int i = 0; i < 4; ++i) for (int j = 0; j < 2; ++j) mp[y+i][x+j] = c;
			} else {
				for (int i = 0; i < 2; ++i) for (int j = 0; j < 4; ++j) mp[y+i][x+j] = c;
			}
		}

		cout << (dfs(sy, sx, mp[sy][sx]) ? "OK" : "NG") << endl;
	}
}