#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

class UnionFind {
    int n;
    vector<int> uni;
    int forest_size;
public:
    explicit UnionFind(int n) : n(n), uni(static_cast<u32>(n), -1), forest_size(n) {};

    int root(int a){
        if (uni[a] < 0) return a;
        else return (uni[a] = root(uni[a]));
    }

    bool unite(int a, int b) {
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(uni[a] > uni[b]) swap(a, b);
        uni[a] += uni[b];
        uni[b] = a;
        forest_size--;
        return true;
    }
    int size(){ return forest_size; }
    int size(int i){ return -uni[root(i)]; }
    bool same(int a, int b) { return root(a) == root(b); }
};

void solve(int h, int w){
    int sy, sx, gy, gx;
    cin >> sx >> sy >> gx >> gy;
    vector<vector<int>> grid(h+2, vector<int>(w+2, 0));
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int c, d, x, y;
        cin >> c >> d >> x >> y;
        if(d){
            for (int j = 0; j < 4; ++j) {
                for (int k = 0; k < 2; ++k) {
                    grid[y+j][x+k] = c;
                }
            }
        }else {
            for (int j = 0; j < 2; ++j) {
                for (int k = 0; k < 4; ++k) {
                    grid[y+j][x+k] = c;
                }
            }
        }
    }
    UnionFind uf((h+2)*(w+2));
    for (int i = 1; i <= h; ++i) {
        for (int j = 1; j <= w; ++j) {
            if(grid[i][j] == grid[i+1][j]){
                uf.unite(i*(w+2)+j, (i+1)*(w+2)+j);
            }
            if(grid[i][j] == grid[i][j+1]){
                uf.unite(i*(w+2)+j, i*(w+2)+j+1);
            }
        }
    }
    puts(uf.same(sy*(w+2)+sx, gy*(w+2)+gx) ? "OK" : "NG");
}

int main() {
    int w, h;
    while(cin >> w >> h, w){
        solve(h, w);
    }
    return 0;
}
