def ext_gcd(a, b): """ return (x, y, gcd(a, b)) s.t. ax + by = gcd(a, b) """ if b == 0: return 1, 0, a else: y, x, g = ext_gcd(b, a % b) return x, y - (a // b) * x, g from math import gcd MOD = 998244353 h, w, n, m, s, c, X, Y = map(int, input().split()) pos = [] for _ in range(n): x, y, a = map(int, input().split()) y = w - y pos.append((x, y, a)) for _ in range(m): x, y, a = map(int, input().split()) y = w - y pos.append((x, y, -a)) Y = w - Y lcm = 4 * h * w // gcd(2 * h, 2 * w) add = [] c0 = 0 c1 = 0 for x, y, a in pos: x_, y_, g = ext_gcd(2 * h, 2 * w) diff = (X - Y) - (x - y) if diff % g == 0: xx = x_ * (diff // g) to_x = x + xx * 2 * h add.append(((to_x - X) % lcm, a)) if a > 0: c0 += 1 else: c1 += 1 diff = (X - Y) - (x - (2 * w - y)) if diff % g == 0: xx = x_ * (diff // g) to_x = x + xx * 2 * h add.append(((to_x - X) % lcm, a)) if a > 0: c0 += 1 else: c1 += 1 diff = (X - Y) - ((2 * h - x) - y) if diff % g == 0: xx = x_ * (diff // g) to_x = 2 * h - x + xx * 2 * h add.append(((to_x - X) % lcm, a)) if a > 0: c0 += 1 else: c1 += 1 diff = (X - Y) - ((2 * h - x) - (2 * w - y)) if diff % g == 0: xx = x_ * (diff // g) to_x = 2 * h - x + xx * 2 * h add.append(((to_x - X) % lcm, a)) if a > 0: c0 += 1 else: c1 += 1 add.sort(key=lambda x: x[0]) tot = 0 min_ = 0 add.append((lcm, 0)) bt = -1 for t, x in add: tot -= (t - 1 + X) // h * c tot -= (t - 1 + Y) // w * c tot += (bt + X) // h * c tot += (bt + Y) // w * c min_ = min(min_, tot) tot += x bt = t if s + min_ > 0 and tot >= 0: print(-1) exit() loop = max(0, (s + min_) // abs(tot)) c0 *= loop c1 *= loop s += loop * tot bt = -1 for t, x in add: diff = 0 diff -= (t - 1 + X) // h * c diff -= (t - 1 + Y) // w * c diff += (bt + X) // h * c diff += (bt + Y) // w * c bt = t if diff + s <= 0: lef = bt rig = t while rig - lef > 1: mid = (rig + lef) // 2 diff = 0 diff -= (mid - 1 + X) // h * c diff -= (mid - 1 + Y) // w * c diff += (bt + X) // h * c diff += (bt + Y) // w * c if diff + s <= 0: rig = mid else: lef = mid xx = (X + rig) % (2 * h) yy = (Y + rig) % (2 * w) if xx >= h: xx = 2 * h - xx if yy >= w: yy = 2 * w - yy yy = w - yy c0 %= MOD c1 %= MOD print(xx, yy, c0, c1) break s += diff s += x if x > 0: c0 += 1 else: c1 += 1 if s <= 0: xx = (X + t) % (2 * h) yy = (Y + t) % (2 * w) if xx >= h: xx = 2 * h - xx if yy >= w: yy = 2 * w - yy yy = w - yy c0 %= MOD c1 %= MOD print(xx, yy, c0, c1) break