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.

Difficulty: interview

Starter code:

Solutions:
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()


#!/usr/bin/env python3

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

# x, y >= 0, x + y > 0
# (d, p, q) : px + qy = d
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):
#	print ('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
	# vx, vy is nonnegative
	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
	# vx = vy > 0
	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
#	print ('nmxypq', n, m, x, y, p, q, '=', p*n - x, q*m - y)
	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)

	


#!/usr/bin/env python2
[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)


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();
#Возвращает НОД d и коэффициенты k1, k2: k1*n1+k2*n2=d
def GCDKoef(n1, n2):
    k11=1;
    k12=0;
    k21=0;
    k22=1;
    while n2>0:
        k, r=divmod(n1, n2);
        n1, n2= n2, r;
        #r=n1-n2*k
        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;
    #t===a1(n1), t===a2(n2), d=GCD(n1, n2), d=n1*k1+n2*k2
    #r=a1%d=a2%d; a1=r+d*x1 a2=r+d*x2
    #t=r+x1*n2*k2+x2*n1*k1===r+d*x1=a1(n1) ===r+d*x2=a2(n2)
    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 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)
    else:
        if (vy == 1):
            print(x, m)
        else:
            print(x, 0)
if (vy == 0):
    if ((y != 0) and (y != m)):
        print(-1)
    else:
        if (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)


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)


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)
  #print(ans)
  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
  #print(xhit, yhit)

  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()