$><<x=y=[0,2].zip(*P=$<.map{|l|l.split.map(&:to_i)<<l}).all?{|f,w,j|l=w,0
(1..P[0][2]).all?{|k|4.times{|i|P[~h=i/2][1][~k]==:UDLR[f+i&=1]&&l[h^=i]=4[g=i*2-l[h]]-g+-2[w+g]}
x,y=l
y<x}&&y<j&&j<=x}?:YES: :NO