import sys
for i, line in enumerate(sys.stdin):
if i%5==0:
b1, b2=line.strip().split()
row, col=int(b1), int(b2)
# step1
if 1<=row<=9 and 1<=col<=9:
print(0)
else:
print(-1)
elif i%5==1:
c1, c2, c3, c4=line.strip().split()
x1, y1, x2, y2=int(c1),int(c2),int(c3),int(c4)
#step2
if 0<=x1<=row-1 and 0<=y1<=col-1 and 0<=x2<=row-1 and 0<=y2<=col-1:
print(0)
else:
print(-1)
elif i%5==2:
insert_row=int(line.strip())
# step3
if 0<=insert_row<=row-1 and row<9:
print(0)
else:
print(-1)
elif i%5==3:
insert_col=int(line.strip())
# step4
if 0<=insert_col<=col-1 and col<9:
print(0)
else:
print(-1)
else:
d1, d2=line.strip().split()
xx1, yy1=int(d1), int(d2)
# step5
if 0<=xx1<=row-1 and 0<=yy1<=col-1:
print(0)
else:
print(-1)