// based on http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2784229#1
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <array>
#include <complex>
#include <utility>
#include <map>
int needleX[4] = { -1,0,1,0 };
int needleY[4] = { 0,1,0,-1 };
bool dfs(int x, int y, int goalx, int goaly, std::vector<std::vector<bool>>&canGo) {
  if (x == goalx && y == goaly) { return true; }
  canGo[y][x] = false;
  for (int i = 0; i < 4; i++) {
    int next_x = x + needleX[i];
    int next_y = y + needleY[i];
 
    if (canGo[next_y][next_x]) {
      bool flag = dfs(next_x, next_y, goalx, goaly, canGo);
      if (flag)
        return true;
    }
  }
  return false;
}
int main(void) {
  int h, w, n;
  std::cin >> w >> h;
  std::vector<std::string> ans;
  while(!(h == 0 && w == 0)){
    int color=-1, orient, leftX, leftY;
    int sx, sy, gx, gy;
    std::cin >> sx >> sy >> gx >> gy;
    std::vector<std::vector<int>> board(h + 2, std::vector<int>(w + 2, 0));
    std::vector<std::vector<bool>> canGo(h + 2, std::vector<bool>(w + 2, false));
    std::cin >> n;
    int start = -1, goal = -1;
    for (int i = 0; i < n; i++) {
      std::cin >> color >> orient >> leftX >> leftY;
 
 
      if (orient == 0) {
        for (int j = 0; j <= 3; j++) {
          for (int k = 0; k <= 1; k++) {
            board[leftY + k][leftX + j] = color;
            if (leftX + j == sx && leftY + k == sy) {
              start = color;
            }
            else if (leftX + j == gx && leftY + k == gy) {
              goal = color;
            }
          }
        }
 
      }
      else {
        for (int j = 0; j <= 1; j++) {
          for (int k = 0; k <= 3; k++) {
            board[leftY + k][leftX + j] = color;
            if (leftX + j == sx && leftY + k == sy) {
              start = color;
            }
            else if (leftX + j == gx && leftY + k == gy) {
              goal = color;
            }
          }
        }
      }
    }

    // for (int i = 0; i <= h; i++) {
    //   for (int j = 0; j <= w; j++) {
    //     std::cerr << board[i][j] << " ";
    //   }
    //   std::cerr << std::endl;
    // }
    
    if (start != -1) {
      for (int i = 0; i <= h; i++) {
        for (int j = 0; j <= w; j++) {
          if (board[i][j] == start) {
            canGo[i][j] = true;
          }
        }
      }
    }

    if (start != goal || start == -1) {
      ans.push_back("NG");
      std::cin >> w >> h;
      continue;
    }

    bool result = dfs(sx, sy, gx, gy, canGo);
    if (result) { ans.push_back("OK"); }
    else { ans.push_back("NG"); }
    std::cin >> w >> h;
  }
  int anssize = ans.size();
  for (int i = 0; i < anssize; i++) {
    std::cout << ans[i] << std::endl;
  }
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <functional>

using namespace std;

#define fr first
#define sc second
#define pb push_back
#define mp make_pair
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define rep(i,x) for(ll i=0;i<x;++i)
#define rep1(i,x) for(ll i=1;i<=x;++i)
#define rrep(i,x) for(ll i=x-1;i>=0;--i)
#define rrep1(i,x) for(ll i=x;i>=1;--i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rev(a) reverse(all(a))

template<typename T> T get(){T a;cin >> a;return a;}

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int inf = 1e9;
const ll linf = 1e18;

int main()
{
    int N; cin >> N;

    vi team(N), rnk(N);

    fill(all(team), 0); fill(all(rnk), 1);

    rep(i, (N*N-N)/2) {
        int a, b, c, d; cin >> a >> b >> c >> d;
        a--; b--;
        if (c > d) team[a]+=3;
        else if (d > c) team[b]+=3;
        else {team[a]++;team[b]++;}
    }

    rep(i, N) rep(j, N) {
        if (team[i] < team[j]) rnk[i]++;
    }

    rep(i, N) cout << rnk[i] << endl;
}