def g;gets.split.map &:to_i;end
def f a,b,c,e,m;[a,b]==e||[[a,b+1],[a,b-1],[a+1,b],[a-1,b]].any?{|i,j|m[j][i]==c&&(m[j][i]=7;f i,j,c,e,m)}end
while(w,h=g)[0]>0
s,t=g
e=g
m=[v=[6]*(w+2)]+(1..h).map{[6]+[0]*w<<6}<<v
g[0].times{c,d,x,y=g;o=d<1?4:2;m[y,6-o].map{|i|i[x,o]=[c]*o}}
puts f(s,t,m[t][s],e,m)?:OK:"NG"end