Consider a billiard table of rectangular size $n \times m$ with four pockets. Let's introduce a coordinate system with the origin at the lower left corner (see the picture).  [Image] 

There is one ball at the point $(x, y)$ currently. Max comes to the table and strikes the ball. The ball starts moving along a line that is parallel to one of the axes or that makes a $45^{\circ}$ angle with them. We will assume that:   the angles between the directions of the ball before and after a collision with a side are equal,  the ball moves indefinitely long, it only stops when it falls into a pocket,  the ball can be considered as a point, it falls into a pocket if and only if its coordinates coincide with one of the pockets,  initially the ball is not in a pocket. 

Note that the ball can move along some side, in this case the ball will just fall into the pocket at the end of the side.

Your task is to determine whether the ball will fall into a pocket eventually, and if yes, which of the four pockets it will be.


-----Input-----

The only line contains $6$ integers $n$, $m$, $x$, $y$, $v_x$, $v_y$ ($1 \leq n, m \leq 10^9$, $0 \leq x \leq n$; $0 \leq y \leq m$; $-1 \leq v_x, v_y \leq 1$; $(v_x, v_y) \neq (0, 0)$) — the width of the table, the length of the table, the $x$-coordinate of the initial position of the ball, the $y$-coordinate of the initial position of the ball, the $x$-component of its initial speed and the $y$-component of its initial speed, respectively. It is guaranteed that the ball is not initially in a pocket.


-----Output-----

Print the coordinates of the pocket the ball will fall into, or $-1$ if the ball will move indefinitely.


-----Examples-----
Input
4 3 2 2 -1 1

Output
0 0
Input
4 4 2 0 1 1

Output
-1
Input
10 10 10 1 -1 0

Output
-1


-----Note-----

The first sample:  [Image] 

The second sample:  [Image] 

In the third sample the ball will never change its $y$ coordinate, so the ball will never fall into a pocket.

Source: codeforces | Difficulty: VERY_HARD
Tags: Geometry

Number theory


Solutions:
import math

def inv(a, m):
	m0 = m
	x0 = 0
	x1 = 1
	if m == 1:
		return 0
	while a > 1:
		if m == 0:
			return -1
		q = a // m
		t = m
		m = a % m
		a = t
		t = x0
		x0 = x1 - q * x0
		x1 = t
	if x1 < 0:
		x1 = x1 + m0
	return x1

def CRT(num, rem):
	k = 2
	prod = 1
	for i in range(0, k):
		prod = prod * num[i]
	result = 0
	for i in range(0, k):
		pp = prod // num[i]
		is_zero = inv(pp, num[i])
		if is_zero == -1:
			return -1
		result = result + rem[i] * inv(pp, num[i]) * pp
	return result % prod

def gcdExtended(a, b):
	if a == 0:
		return (b, 0, 1)
	(gcd, x1, y1) = gcdExtended(b % a, a)
	x = y1 - b // a * x1
	y = x1
	return (gcd, x, y)

def main():
	(n, m, x, y, vx, vy) = [int(x) for x in input().split(' ')]
	if vx == 0 and x != 0 and (x != n):
		return -1
	elif vx == 0:
		if vy == 0:
			return -1
		elif vy == 1:
			return str(x) + ' ' + str(m)
		elif vy == -1:
			return str(x) + ' ' + str(0)
	if vy == 0 and y != 0 and (y != m):
		return -1
	elif vy == 0:
		if vx == 0:
			return -1
		elif vx == 1:
			return str(n) + ' ' + str(y)
		elif vx == -1:
			return str(0) + ' ' + str(y)
	if vx == 1:
		rest1 = n - x
	else:
		rest1 = x
	if vy == 1:
		rest2 = m - y
	else:
		rest2 = y
	nk = [n, m]
	rest = [rest1, rest2]
	g = math.gcd(n, m)
	if g == 1:
		t = CRT(nk, rest)
	elif rest1 % g != rest2 % g:
		return -1
	else:
		(g, p, q) = gcdExtended(n, m)
		mod = m // g
		lcm = n * mod
		t = rest1 % lcm + n * (p % mod * ((rest2 - rest1) // g % mod) % mod) % lcm
	if t == -1:
		return -1
	else:
		kx = (t - rest1) / n
		ky = (t - rest2) / m
		izq = False
		up = False
		if vx == 1:
			if kx % 2 == 1:
				izq = True
			else:
				izq = False
		elif vx == -1:
			if kx % 2 == 1:
				izq = False
			else:
				izq = True
		if vy == 1:
			if ky % 2 == 1:
				up = False
			else:
				up = True
		elif vy == -1:
			if ky % 2 == 1:
				up = True
			else:
				up = False
		if up and izq:
			return str(0) + ' ' + str(m)
		elif not up and (not izq):
			return str(n) + ' 0'
		elif up and (not izq):
			return str(n) + ' ' + str(m)
		else:
			return '0 0'
print(main())


from sys import stderr

def gcd_ext(a, b):
	(r2, x2, y2, r1, x1, y1, r0, x0, y0, q) = 10 * [0]
	r2 = a
	x2 = 1
	y2 = 0
	r1 = b
	x1 = 0
	y1 = 1
	while r1:
		q = r2 // r1
		r0 = r2 % r1
		x0 = x2 - q * x1
		y0 = y2 - q * y1
		r2 = r1
		x2 = x1
		y2 = y1
		r1 = r0
		x1 = x0
		y1 = y0
	g = r2
	x = x2
	y = y2
	if g < 0:
		g = -g
		x = -x
		y = -y
	return (g, x, y)

def crt(r1, m1, r2, m2):
	(g, x, y) = gcd_ext(m1, m2)
	if (r1 - r2) % g != 0:
		return (-1, -1)
	z = m2 // g
	lcm = m1 * z
	sol = r1 % lcm + m1 * (x % z * ((r2 - r1) // g % z) % z) % lcm
	return (sol, lcm)
(n, m, x, y, vx, vy) = list(map(int, input().split()))
if vx == 0 and x % n != 0 or (vy == 0 and y % m != 0):
	print(-1)
	exit(0)
if vx == 0:
	print(x, (vy + 1) // 2 * m)
	exit(0)
if vy == 0:
	print((vx + 1) // 2 * n, y)
	exit(0)
if x == 0 and vx == -1 or (x == n and vx == 1):
	vx *= -1
if y == 0 and vy == -1 or (y == m and vy == 1):
	vy *= -1
r1 = x * -vx
r2 = y * -vy
if vx > 0:
	r1 += n
if vy > 0:
	r2 += m
(sol, asdf) = crt(r1, n, r2, m)
if sol == -1 and asdf == -1:
	print(-1)
	exit(0)
k1 = (sol - r1) // n
k2 = (sol - r2) // m
if k1 % 2:
	vx *= -1
if k2 % 2:
	vy *= -1
print((vx + 1) // 2 * n, (vy + 1) // 2 * m)


[n, m, x, y, vx, vy] = list(map(int, input().strip().split()))

def mygcd(x, y):
	if x > y:
		(d, p, q) = mygcd(y, x)
		return (d, q, p)
	if x == 0:
		return (y, 0, 1)
	(m, r) = divmod(y, x)
	(d, p, q) = mygcd(r, x)
	return (d, q - p * m, p)

def solve(n, m, x, y, vx, vy):
	if vx < 0:
		(cx, cy) = solve(n, m, n - x, y, -vx, vy)
		if cx < 0:
			return (cx, cy)
		else:
			return (n - cx, cy)
	if vy < 0:
		(cx, cy) = solve(n, m, x, m - y, vx, -vy)
		if cx < 0:
			return (cx, cy)
		else:
			return (cx, m - cy)
	if vx == 0:
		if x in (0, n):
			return (x, m)
		else:
			return (-1, -1)
	if vy == 0:
		if y in (0, m):
			return (n, y)
		else:
			return (-1, -1)
	(d, p, q) = mygcd(n, m)
	if (x - y) % d != 0:
		return (-1, -1)
	if x == y and n == m:
		return (n, m)
	if x == y:
		return (m // d % 2 * n, n // d % 2 * m)
	mxy = min(x, y)
	(x, y) = (x - mxy, y - mxy)
	n //= d
	m //= d
	x //= d
	y //= d
	c = x - y
	p = p * c % m
	q = -q * c % n
	if p * n - x < 0:
		p += m * -((x - p * n) // m)
	if q * m - y < 0:
		q += n * -((y - q * m) // n)
	return (p % 2 * n * d, q % 2 * m * d)
(cx, cy) = solve(n, m, x, y, vx, vy)
if cx < 0:
	print(-1)
else:
	print(cx, cy)


(n, m, x, y, vx, vy) = map(int, input().split())

def ex():
	print(-1)
	exit(0)

def output(x, y):
	if x % n != 0 or y % m != 0:
		ex()
	x = x // n % 2 * n
	y = y // m % 2 * m
	print(x, y)
	exit(0)

def print_step(k):
	output(x + k * vx, y + k * vy)

def ex_gcd(m, n, c):
	(x, y, x1, y1) = (0, 1, 1, 0)
	nn = m
	while m % n:
		(x, x1) = (x1 - m // n * x, x)
		(y, y1) = (y1 - m // n * y, y)
		(m, n) = (n, m % n)
	if c % n != 0:
		ex()
	return (c // n * y, nn // n)
if vx == 0:
	if x % n != 0:
		ex()
	print_step(y // -vy % m)
if vy == 0:
	if y % m != 0:
		ex()
	print_step(x // -vx % m)
(a, b) = ex_gcd(n, -vx, x)
(c, d) = ex_gcd(m, -vy, y)
(e, f) = ex_gcd(b, -d, c - a)
g = c + d * e
h = d * f
if h == 0:
	if g <= 0:
		ex()
	print_step(g)
print_step(g % abs(h))


def mod(x, m):
	return x % m

def mul(x, y, m):
	return x * y % m

def add(x, y, m):
	return (x + y) % m

def gcdext(a, b):
	if b == 0:
		return (a, 1, 0)
	(g, y, x) = gcdext(b, a % b)
	return (g, x, y - x * (a // b))

def CRT(r1, m1, r2, m2):
	(g, x, y) = gcdext(m1, m2)
	if (r1 - r2) % g:
		return (-1, -1)
	z = m2 // g
	lcm = m1 * z
	sol = add(mod(r1, lcm), m1 * mul(mod(x, z), mod((r2 - r1) // g, z), z), lcm)
	return (sol, lcm)
(n, m, x, y, vx, vy) = [int(x) for x in input().split()]
if not vx and x and (x != n) or (not vy and y and (y != m)):
	print(-1)
	exit()
if not x and vx:
	vx = 1
if x == n and vx:
	vx = -1
if not y and vy:
	vy = 1
if y == m and vy:
	vy = -1
dx = x
if vx > 0:
	dx = n - x
dy = y
if vy > 0:
	dy = m - y
if not x or x == n:
	dx = 0
if not y or y == m:
	dy = 0
if not vx:
	if vy > 0:
		print(x, m)
	else:
		print(x, 0)
	exit()
if not vy:
	if vx > 0:
		print(n, y)
	else:
		print(0, y)
	exit()
res = CRT(dx, n, dy, m)
if res[0] == -1:
	print(-1)
	exit()
t = res[0] + res[1]
choques_y = (res[0] - dy) // m
choques_x = (res[0] - dx) // n
if not x or x == n:
	choques_x += 1
if not y or y == m:
	choques_y += 1
ansx = 0
if vx > 0 and choques_x % 2 == 0:
	ansx = n
elif vx < 0 and choques_x % 2:
	ansx = n
ansy = 0
if vy > 0 and choques_y % 2 == 0:
	ansy = m
elif vy < 0 and choques_y % 2:
	ansy = m
print(ansx, ansy)


import sys

def parser():
	while 1:
		data = list(input().split(' '))
		for number in data:
			if len(number) > 0:
				yield number
input_parser = parser()

def get_word():
	global input_parser
	return next(input_parser)

def get_number():
	data = get_word()
	try:
		return int(data)
	except ValueError:
		return float(data)

def done(x, y):
	print('%d %d' % (x, y))
	sys.exit(0)

def die():
	print(-1)
	sys.exit(0)

def ext_gcd(a, b):
	if b != 0:
		tmp = ext_gcd(b, a % b)
		return [tmp[1], tmp[0] - a // b * tmp[1]]
	return [1, 0]

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
n = get_number()
m = get_number()
x = get_number()
y = get_number()
vx = get_number()
vy = get_number()
if vx == 0:
	if x != 0 and x != n:
		die()
	if x == 0:
		if vy > 0:
			done(0, m)
		else:
			done(0, 0)
	if x == n:
		if vy > 0:
			done(n, m)
		else:
			done(n, 0)
if vy == 0:
	if y != 0 and y != m:
		die()
	if y == 0:
		if vx > 0:
			done(n, 0)
		else:
			done(0, 0)
	if y == m:
		if vx > 0:
			done(n, m)
		else:
			done(0, m)
g = gcd(vy * n, vx * m)
if (vy * x - vx * y) % g != 0:
	die()
a = vy * n // g
b = vx * m // g
t = (vy * x - vx * y) // g
res = ext_gcd(a, -b)
if res[0] * a + res[1] * -b == -1:
	res[0] = -res[0]
	res[1] = -res[1]
alpha = res[0] * t
beta = res[1] * t
k = b // gcd(a, b)
T = (n * alpha - x) // vx
val = 0
if vx * k > 0:
	val = 1
else:
	val = -1
high = 98765432198765432198765432199
low = 0
ans = 0
while high >= low:
	mid = (high + low) // 2
	nalpha = alpha + mid * val * k
	nT = (n * nalpha - x) // vx
	if nT > 0:
		ans = mid
		high = mid - 1
	else:
		low = mid + 1
alpha = alpha + ans * val * k
T = (n * alpha - x) // vx
val = 0
if vx * k > 0:
	val = -1
else:
	val = 1
high = 98765432198765432198765432199
low = 0
ans = 0
while high >= low:
	mid = (high + low) // 2
	nalpha = alpha + mid * val * k
	nT = (n * nalpha - x) // vx
	if nT >= 0:
		ans = mid
		low = mid + 1
	else:
		high = mid - 1
alpha = alpha + ans * val * k
T = (n * alpha - x) // vx
X = x + vx * T
Y = y + vy * T
T = t + vy * T
if X // n % 2 == 0 and Y // m % 2 == 0:
	done(0, 0)
elif X // n % 2 == 0:
	done(0, m)
elif Y // m % 2 == 0:
	done(n, 0)
else:
	done(n, m)


def mcd(a, b):
	if a == 0 and b == 0:
		print('ERROR')
	elif a == 0:
		return b
	elif b == 0:
		return a
	elif a >= b:
		return mcd(b, a % b)
	else:
		return mcd(a, b % a)

def floor(a):
	return int(a // 1)

def inverso(a, n):
	if mcd(a, n) > 1:
		raise Exception('No existe inverso')
	else:
		(s0, t0, s1, t1, r0, r1) = (1, 0, 0, 1, n, a)
	while r1 > 1:
		aux_s = s0 - floor(r0 / r1) * s1
		s0 = s1
		s1 = aux_s
		aux_t = t0 - floor(r0 / r1) * t1
		t0 = t1
		t1 = aux_t
		r0 = r1
		r1 = s1 * n + t1 * a
	if t1 < 0:
		t1 += n
	return t1
inp = input().split(' ')
inp = [int(x) for x in inp]
(n, m, x, y, vx, vy) = inp
if (x == 0 or x == n) and vx == 0 and (vy != 0):
	if abs(vy) / vy == 1:
		print(x, m)
	else:
		print(x, 0)
elif (y == 0 or y == m) and vy == 0 and (vx != 0):
	if abs(vx) / vx == 1:
		print(n, y)
	else:
		print(0, y)
elif (x == 0 or x == n) and vx != 0 and (vy == 0):
	print(-1)
elif (y == 0 or y == m) and vy != 0 and (vx == 0):
	print(-1)
elif (x != 0 and y != 0 and (x != n) and (y != m)) and (vx != 0 and vy == 0 or (vy != 0 and vx == 0)):
	print(-1)
elif vx == 0 and vy == 0:
	print(-1)
elif vx != 0 and vy != 0:
	if vy > 0:
		a1 = m - y
	else:
		a1 = y
	if vx > 0:
		a2 = n - x
	else:
		a2 = x
	if mcd(n, m) == 1:
		dist = (a1 * n * inverso(n, m) + a2 * m * inverso(m, n)) % (n * m)
		reb_y = (dist - a1) / m
		reb_x = (dist - a2) / n
		if vy > 0 and reb_y % 2 != 0 or (vy < 0 and reb_y % 2 == 0):
			up_down = 0
		else:
			up_down = m
		if vx > 0 and reb_x % 2 != 0 or (vx < 0 and reb_x % 2 == 0):
			right_left = 0
		else:
			right_left = n
		print(right_left, up_down)
	elif (a1 - a2) % mcd(m, n) != 0:
		print(-1)
	else:
		try:
			(g, n_aux, m_aux) = (mcd(n, m), n, m)
			if mcd(n // g, m) == 1:
				n_aux = n // g
			else:
				m_aux = m // g
			dist = (a1 * n_aux * inverso(n_aux, m_aux) + a2 * m_aux * inverso(m_aux, n_aux)) % (n_aux * m_aux)
			reb_y = (dist - a1) / m
			reb_x = (dist - a2) / n
			if vy > 0 and reb_y % 2 != 0 or (vy < 0 and reb_y % 2 == 0):
				up_down = 0
			else:
				up_down = m
			if vx > 0 and reb_x % 2 != 0 or (vx < 0 and reb_x % 2 == 0):
				right_left = 0
			else:
				right_left = n
			print(right_left, up_down)
		except:
			print(-1)


import fractions

def extendedEuclid(A, B):
	X = 0
	Y = 0
	if B == 0:
		X = 1
		Y = 0
	else:
		(X, Y) = extendedEuclid(B, A % B)
		temp = X
		X = Y
		Y = temp - A // B * Y
	return (X, Y)

def DiophantusSolver(a, b, c):
	g = abs(fractions.gcd(a, b))
	if c % g != 0:
		return (10 ** 40, 10 ** 40)
	(p, q) = extendedEuclid(abs(a // g), abs(b // g))
	r11 = p * c // g
	r12 = q * c // g
	t1 = xi + r11 * n
	(q, p) = extendedEuclid(abs(b // g), abs(a // g))
	r21 = p * c // g
	r22 = q * c // g
	t2 = xi + r21 * n
	if (r11, r12) < (r21, r22):
		return (r11, r12)
	return (r21, r22)
(n, m, x, y, vx, vy) = map(int, input().split())
if vx * vy == 0:
	if vx == 0:
		if x > 0 and x < n:
			print('-1')
		elif vy < 0:
			print('{} 0'.format(x))
		elif vy > 0:
			print('{} {}'.format(x, m))
	elif vy == 0:
		if y > 0 and y < m:
			print('-1')
		elif vx < 0:
			print('0 {}'.format(y))
		elif vx > 0:
			print('{} {}'.format(n, y))
else:
	xi = x % n
	yi = y % m
	LCM = n * m // fractions.gcd(n, m)
	if vx > 0:
		xi = (n - x) % n
	if vy > 0:
		yi = (m - y) % m
	(tx, ty) = DiophantusSolver(n, -m, yi - xi)
	if tx == 10 ** 40:
		print('-1')
	else:
		t = (xi + tx * n) % LCM
		while t < 0:
			t += LCM
		x1 = (x + t * vx) % (n * 2)
		while x1 < 0:
			x1 += n * 2
		y1 = (y + t * vy) % (m * 2)
		while y1 < 0:
			y1 += m * 2
		print(x1, y1)


from functools import reduce
import math

def bezout(a, b):
	if a == 0:
		return (0, 1)
	else:
		(x, y) = bezout(b % a, a)
		return (y - b // a * x, x)

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def CRT(a1, m1, a2, m2):
	g = math.gcd(m1, m2)
	if (a1 - a2) % g != 0:
		return -1
	(p, q) = bezout(m1, m2)
	r = a2 * (m1 // g * p) + a1 * (m2 // g * q)
	return r % lcm(m1, m2)

def main(data):
	(n, m, x, y, dx, dy) = data.split()
	(n, m, x, y, dx, dy) = (int(n), int(m), int(x), int(y), int(dx), int(dy))
	if dy == 0:
		if 0 < y < m:
			return -1
		if dx > 0:
			return f'{n} {y}'
		return f'{0} {y}'
	if dx == 0:
		if 0 < x < n:
			return -1
		if dy > 0:
			return f'{x} {m}'
		return f'{x} {0}'
	(n, rn, m, rm) = (n, int(-x / dx % n), m, int(-y / dy % m))
	a = CRT(rn, n, rm, m)
	if a < 1:
		return -1
	(rx, ry) = (x + a * dx, y + a * dy)
	if (rx, ry) in {(0, 0), (n, 0), (0, m), (n, m)}:
		return f'{int(rx)} {int(ry)}'
	(tx, ty) = (rx // n, ry // m)
	if rx > 0:
		tx -= 1
	if ry > 0:
		ty -= 1
	(a, b, c, d) = ('0 0', f'0 {m}', f'{n} 0', f'{n} {m}')
	if tx <= 0 and ty < 0:
		(tx, ty) = (tx % 2, ty % 2)
		if (tx, ty) == (0, 0):
			return a
		elif (tx, ty) == (0, 1):
			return b
		elif (tx, ty) == (1, 0):
			return c
		elif (tx, ty) == (1, 1):
			return d
	elif 0 <= tx and ty < 0:
		(tx, ty) = (tx % 2, ty % 2)
		if (tx, ty) == (0, 0):
			return c
		elif (tx, ty) == (0, 1):
			return d
		elif (tx, ty) == (1, 0):
			return a
		elif (tx, ty) == (1, 1):
			return b
	elif tx <= 0 and 0 <= ty:
		(tx, ty) = (tx % 2, ty % 2)
		if (tx, ty) == (0, 0):
			return b
		elif (tx, ty) == (0, 1):
			return a
		elif (tx, ty) == (1, 0):
			return d
		elif (tx, ty) == (1, 1):
			return c
	elif 0 <= tx and 0 <= ty:
		(tx, ty) = (tx % 2, ty % 2)
		if (tx, ty) == (0, 0):
			return d
		elif (tx, ty) == (0, 1):
			return c
		elif (tx, ty) == (1, 0):
			return b
		elif (tx, ty) == (1, 1):
			return a
data = input().strip()
print(main(data))


def extended_gcd(a, b):
	r_last = a
	r = b
	s_last = 1
	s = 0
	t_last = 0
	t = 1
	while r:
		rr = r
		rr_last = r_last
		r = r_last % r
		r_last = rr
		ss = s
		s = s_last - rr_last // rr * s
		s_last = ss
		tt = t
		t = t_last - rr_last // rr * t
		t_last = tt
	return [r_last, s_last, t_last]

def mod_inverse(a, n):
	(g, s, t) = extended_gcd(a, n)
	print(a, n, s, t)
	return (s + n) % n

def CRT(a, b, m, n):
	(g, p, q) = extended_gcd(m, n)
	if (a - b) % g != 0:
		return 'NO'
	lcm = m * n // g
	ans = a * n * q // g + b * m * p // g
	return (ans + lcm) % lcm
(m, n, x, y, vx, vy) = map(int, input().split(' '))
if vx == 0 and x % m != 0 or (vy == 0 and y % n != 0) or (vx == 0 and vy == 0):
	print(-1)
	quit()
if vx == 0:
	yy = n if vy == 1 else 0
	print(x, yy)
	quit()
if vy == 0:
	xx = n if vx == 1 else 0
	print(xx, y)
	quit()
c = CRT(-1 * x * vx, -1 * y * vy, m, n)
if c == 'NO':
	print(-1)
else:
	first_bounce_x = x * (vx == -1) + (m - x) * (vx == 1)
	first_bounce_y = y * (vy == -1) + (n - y) * (vy == 1)
	first_bounce_dist = min(first_bounce_x, first_bounce_y)
	hor_bounces = (c - first_bounce_dist) // m
	vert_bounces = (c - first_bounce_dist) // n
	hor_side = hor_bounces % 2
	vert_side = vert_bounces % 2
	h = v = -1
	if hor_side == 0 and vx == -1:
		h = 0
	elif hor_side == 1 and vx == -1:
		h = 1
	elif hor_side == 0 and vx == 1:
		h = 1
	else:
		h = 0
	if vert_side == 0 and vy == -1:
		v = 0
	elif vert_side == 1 and vy == -1:
		v = 1
	elif vert_side == 0 and vy == 1:
		v = 1
	else:
		v = 0
	print(h * m, v * n)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def exgcd(a, b):
	if b == 0:
		return (1, 0, a)
	(p, q, r) = exgcd(b, a % b)
	t = p
	p = q
	q = t - a // b * q
	return (p, q, r)
list = input().split()
n = int(list[0])
m = int(list[1])
x = int(list[2])
y = int(list[3])
vx = int(list[4])
vy = int(list[5])
if vx == 0:
	if x != 0 and x != n:
		print(-1)
	elif vy == 1:
		print(x, m)
	else:
		print(x, 0)
if vy == 0:
	if y != 0 and y != m:
		print(-1)
	elif vx == 1:
		print(n, y)
	else:
		print(0, y)
if vx != 0 and vy != 0:
	fx = 0
	fy = 0
	if vx == -1:
		fx = 1
		x = n - x
	if vy == -1:
		fy = 1
		y = m - y
	(p, q, r) = exgcd(n, m)
	if (x - y) % r != 0:
		print(-1)
	else:
		p *= (x - y) // r
		q *= (x - y) // r
		mn = m * n // gcd(m, n)
		a = (p * n - x) % mn
		while a <= 0:
			a += mn
		p = (x + a) // n
		q = (y + a) // m
		ansx = 0
		ansy = 0
		if (p + fx) % 2 == 1:
			ansx = n
		if (q + fy) % 2 == 1:
			ansy = m
		print(ansx, ansy)


def INV(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x

def GCD(a, b):
	if a == 0:
		return b
	return GCD(b % a, a)

def solve():
	(n, m, x, y, vx, vy) = list(map(int, input().split()))
	if vx == 0:
		if x != 0 and x != n:
			print('-1')
			return 0
		ans = [0, 0]
		ans[0] = x
		if vy < 0:
			ans[1] = 0
		else:
			ans[1] = m
		print(ans[0], ans[1])
		return 0
	if vy == 0:
		if y != 0 and y != m:
			print('-1')
			return 0
		ans = [0, 0]
		ans[1] = y
		if vx < 0:
			ans[0] = 0
		else:
			ans[0] = n
		print(ans[0], ans[1])
		return 0
	if vx == -1 and x == 0:
		vx = 1
	if vx == 1 and x == n:
		vx = -1
	if vy == -1 and y == 0:
		vy = 1
	if vy == 1 and y == m:
		vy = -1
	g = GCD(n, m)
	m1 = n
	m2 = m
	a1 = -1 * vx * x % m1
	a2 = -1 * vy * y % m2
	if a1 % g != a2 % g:
		print('-1')
		return 0
	if GCD(m1 // g, m2) == 1:
		m1 = m1 // g
	else:
		m2 = m2 // g
	m1_bar = INV(m2, m1)
	m2_bar = INV(m1, m2)
	k = (a1 * m1_bar * m2 + a2 * m2_bar * m1) % (m1 * m2)
	rowtouch = (k + m - 1) // m % 2
	coltouch = (k + n - 1) // n % 2
	ans = [0, 0]
	if vx == 1:
		ans[0] = 0
	else:
		ans[0] = n
	if vy == 1:
		ans[1] = 0
	else:
		ans[1] = m
	if rowtouch == 1:
		ans[1] = m - ans[1]
	if coltouch == 1:
		ans[0] = n - ans[0]
	print(ans[0], ans[1])
	return 0
solve()


from math import floor, ceil
from sys import stdout

def Billiard_Table(n, m, x, y, vx, vy):
	if n - x == m - y:
		return (n, m)
	r = y - x
	result = GCD_Euclidean_Algorithm(m, n)
	d = result[0]
	k2_0 = result[1]
	k1_0 = -result[2]
	if r % d != 0:
		return (-1, 'A')
	k1_0 = k1_0 * r // d
	k2_0 = k2_0 * r // d
	t_lb1 = -k1_0 / m * d
	t_lb2 = -k2_0 / n * d
	t = min(t_lb1, t_lb2)
	if t - floor(t) == 0:
		t += 1
	t = ceil(t)
	k1 = k1_0 + t * m // d
	k2 = k2_0 + t * n // d
	return (int(k1) % 2 * n, int(k2) % 2 * m)

def GCD_Euclidean_Algorithm(a, b):
	if a == 0:
		return (b, 0, 1)
	(d, x1, y1) = GCD_Euclidean_Algorithm(b % a, a)
	x = y1 - floor(b / a) * x1
	y = x1
	return (d, x, y)

def main():
	(n, m, x, y, vx, vy) = input().split(' ')
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	vx = int(vx)
	vy = int(vy)
	if vx == 0:
		if vy == 0:
			stdout.write(str(-1))
			return
		if x == 0:
			if vy > 0:
				stdout.write(str(0) + ' ' + str(m))
				return
			elif vy < 0:
				stdout.write(str(0) + ' ' + str(0))
				return
		elif x == m:
			if vy < 0:
				stdout.write(str(n) + ' ' + str(0))
				return
			elif vy > 0:
				stdout.write(str(n) + ' ' + str(m))
				return
		else:
			stdout.write(str(-1))
			return
	if vy == 0:
		if y == 0:
			if vx > 0:
				stdout.write(str(n) + ' ' + str(0))
				return
			elif vx < 0:
				stdout.write(str(0) + ' ' + str(0))
				return
		elif y == m:
			if vx > 0:
				stdout.write(str(n) + ' ' + str(m))
				return
			elif vx < 0:
				stdout.write(str(0) + ' ' + str(m))
				return
		else:
			stdout.write(str(-1))
			return
	if vx == 1 and vy == 1:
		(k1, k2) = Billiard_Table(n, m, x, y, vx, vy)
		if k2 == 'A':
			stdout.write(str(-1))
			return
	elif vx == 1 and vy == -1:
		vy_prime = 1
		y_prime = -y + m
		(k1, k2) = Billiard_Table(n, m, x, y_prime, vx, vy_prime)
		if k2 == 'A':
			stdout.write(str(-1))
			return
		k2 = -(k2 - m)
	elif vx == -1 and vy == 1:
		vx_prime = 1
		x_prime = -x + n
		(k1, k2) = Billiard_Table(n, m, x_prime, y, vx_prime, vy)
		if k2 == 'A':
			stdout.write(str(-1))
			return
		k1 = -(k1 - n)
	else:
		vy_prime = 1
		y_prime = -y + m
		vx_prime = 1
		x_prime = -x + n
		(k1, k2) = Billiard_Table(n, m, x_prime, y_prime, vx_prime, vy_prime)
		if k2 == 'A':
			stdout.write(str(-1))
			return
		k1 = -(k1 - n)
		k2 = -(k2 - m)
	stdout.write(str(k1) + ' ' + str(k2))
	return
main()


def getIntList():
	return list(map(int, input().split()))

def getTransIntList(n):
	first = getIntList()
	m = len(first)
	result = [[0] * n for _ in range(m)]
	for i in range(m):
		result[i][0] = first[i]
	for j in range(1, n):
		curr = getIntList()
		for i in range(m):
			result[i][j] = curr[i]
	return result
(n, m, x, y, vx, vy) = getIntList()

def GCDKoef(n1, n2):
	k11 = 1
	k12 = 0
	k21 = 0
	k22 = 1
	while n2 > 0:
		(k, r) = divmod(n1, n2)
		(n1, n2) = (n2, r)
		(k11, k12, k21, k22) = (k21, k22, k11 - k21 * k, k12 - k22 * k)
	return (n1, k11, k12)

def solveSystemCongruence(a1, n1, a2, n2):
	(d, k1, k2) = GCDKoef(n1, n2)
	if (a1 - a2) % d != 0:
		return None
	r = a1 % d
	x1 = (a1 - r) // d
	x2 = (a2 - r) // d
	t = r + x1 * n2 * k2 + x2 * n1 * k1
	mod = n1 * n2 // d
	t %= mod
	if t < 0:
		t += mod
	return t
if vx == 0:
	if x != 0 and x != n:
		print(-1)
	elif vy == 1:
		print(x, m)
	else:
		print(x, 0)
elif vy == 0:
	if y != 0 and y != m:
		print(-1)
	elif vx == 1:
		print(n, y)
	else:
		print(0, y)
else:
	t = solveSystemCongruence(-x * vx, n, -y * vy, m)
	if t == None:
		print(-1)
	else:
		x = x + t * vx
		y = y + t * vy
		x //= n
		y //= m
		x %= 2
		y %= 2
		x = abs(x)
		y = abs(y)
		print(x * n, y * m)


def exgcd(a, b):
	if b == 0:
		return (a, 1, 0)
	(g, y, x) = exgcd(b, a % b)
	return (g, x, y - x * (a // b))

def crt(a1, m1, a2, m2):
	g = exgcd(m1, m2)[0]
	(e1, e2) = (a1 % g, a2 % g)
	if e1 != e2:
		return None
	a1 //= g
	m1 //= g
	a2 //= g
	m2 //= g
	(_, m1i, m2i) = exgcd(m1, m2)
	ans = a1 * m2 * m2i + a2 * m1 * m1i
	ans = ans % (m1 * m2)
	if ans < 0:
		ans += m1 * m2
	return ans * g + e1

def solve(N, M, x, y, vx, vy):
	if vx == 0:
		if x == 0 or x == N:
			return (x, 0)
		else:
			return None
	if vy == 0:
		if y == 0 or y == M:
			return (0, y)
		else:
			return None
	ans = crt(x, N, y, M)
	if ans is None:
		return ans
	(xhit, yhit) = ((ans + N - 1) // N, (ans + M - 1) // M)
	if x == 0:
		xhit -= 1
	if y == 0:
		yhit -= 1
	return (N if xhit % 2 == 0 else 0, M if yhit % 2 == 0 else 0)

def main():
	(N, M, x, y, vx, vy) = map(int, input().split())
	(swapx, swapy) = (False, False)
	if vx > 0:
		swapx = True
		x = N - x
	if vy > 0:
		swapy = True
		y = M - y
	ans = solve(N, M, x, y, vx, vy)
	if ans is None:
		print(-1)
	else:
		(ax, ay) = ans
		if swapx:
			ax = N - ax
		if swapy:
			ay = M - ay
		print(ax, ay)

def __starting_point():
	main()
__starting_point()


(n, m, x, y, dx, dy) = list(map(int, input().split()))

def trivial():
	if dx == 0:
		if x != 0 and x != n:
			print(-1)
		elif dy == 1:
			print(x, m)
		else:
			print(x, 0)
		exit(0)
	if dy == 0:
		if y != 0 and y != m:
			print(-1)
		elif dx == 1:
			print(n, y)
		else:
			print(0, y)
		exit(0)

def write(ans, x, y, n, m, dx, dy):
	x = (ans + x) // n
	y = (ans + y) // m
	if x % 2 == 0:
		x = 0
	else:
		x = n
	if y % 2 == 0:
		y = 0
	else:
		y = m
	if dx == -1:
		x = n - x
	if dy == -1:
		y = m - y
	print(x, y)
	exit(0)

def euclid(x, y):
	if x == 0:
		return (y, 0, 1)
	(g, qq, ww) = euclid(y % x, x)
	q = ww - y // x * qq
	w = qq
	return (g, q, w)
trivial()
if dx == -1:
	x = n - x
if dy == -1:
	y = m - y
(gcd, q, w) = euclid(n, m)
w *= -1
diff = x - y
if diff % gcd:
	print(-1)
	exit(0)
q *= diff // gcd
w *= diff // gcd
ans = n * q - x
pl = n * m // gcd
ans %= pl
if ans < 0:
	ans += pl
write(ans, x, y, n, m, dx, dy)


INF = 1e+19

def mod(x, m):
	x %= m
	return x + m if x < 0 else x

def gcdext(a, b):
	(r2, x2, y2) = (a, 1, 0)
	(r1, x1, y1) = (b, 0, 1)
	while r1 > 0:
		q = r2 // r1
		r0 = r2 % r1
		x0 = x2 - q * x1
		y0 = y2 - q * y1
		(r2, x2, y2) = (r1, x1, y1)
		(r1, x1, y1) = (r0, x0, y0)
	(g, x, y) = (r2, x2, y2)
	if g < 0:
		(g, x, y) = (-g, -x, -y)
	return (g, x, y)

def lincongeq(a, b, m):
	a = mod(a, m)
	b = mod(b, m)
	(g, s, t) = gcdext(a, m)
	if b % g == 0:
		bb = b // g
		mm = m // g
		n = -s * bb // mm
		x = s * bb + n * mm
		if x < 0:
			x += mm
		return (True, x)
	return False

def main():
	(w, h, x0, y0, vx, vy) = map(int, input().split())
	t_min = INF
	if vx != 0:
		if vx > 0:
			hsteps20 = 2 * w - x0
			hsteps2w = w - x0
		else:
			hsteps20 = x0
			hsteps2w = x0 + w
		a = vy * 2 * w
		b = -y0 - vy * hsteps20
		ret = lincongeq(a, b, 2 * h)
		if ret:
			k = ret[1]
			t = 2 * w * k + hsteps20
			if t_min > t:
				(t_min, xf, yf) = (t, 0, 0)
		a = vy * 2 * w
		b = h - y0 - vy * hsteps20
		ret = lincongeq(a, b, 2 * h)
		if ret:
			k = ret[1]
			t = 2 * w * k + hsteps20
			if t_min > t:
				(t_min, xf, yf) = (t, 0, h)
		a = vy * 2 * w
		b = -y0 - vy * hsteps2w
		ret = lincongeq(a, b, 2 * h)
		if ret:
			k = ret[1]
			t = 2 * w * k + hsteps2w
			if t_min > t:
				(t_min, xf, yf) = (t, w, 0)
		a = vy * 2 * w
		b = h - y0 - vy * hsteps2w
		ret = lincongeq(a, b, 2 * h)
		if ret:
			k = ret[1]
			t = 2 * w * k + hsteps2w
			if t_min > t:
				(t_min, xf, yf) = (t, w, h)
	elif (x0 == 0 or x0 == w) and vy != 0:
		t_min = 0
		xf = x0
		yf = h if vy > 0 else 0
	if t_min == INF:
		print('-1')
	else:
		print('%d %d' % (xf, yf))
main()