Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n × m chessboard, a very tasty candy and two numbers a and b.

Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:

  move the candy from position (x, y) on the board to position (x - a, y - b);  move the candy from position (x, y) on the board to position (x + a, y - b);  move the candy from position (x, y) on the board to position (x - a, y + b);  move the candy from position (x, y) on the board to position (x + a, y + b). 

Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.

Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task! 


-----Input-----

The first line of the input contains six integers n, m, i, j, a, b (1 ≤ n, m ≤ 10^6; 1 ≤ i ≤ n; 1 ≤ j ≤ m; 1 ≤ a, b ≤ 10^6).

You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).


-----Output-----

In a single line print a single integer — the minimum number of moves needed to get the candy.

If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.


-----Examples-----
Input
5 7 1 3 2 2

Output
2

Input
5 5 2 3 1 1

Output
Poor Inna and pony!



-----Note-----

Note to sample 1:

Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.

Source: codeforces | Difficulty: HARD
Tags: Implementation

Greedy algorithms


Solutions:
def fun(x, y):
	if abs(x - i) % a == 0 and abs(y - j) % b == 0 and ((abs(x - i) // a - abs(y - j) // b) % 2 == 0):
		return max(abs(x - i) // a, abs(y - j) // b)
	return 1000000000
(n, m, i, j, a, b) = map(int, input().split())
ans = min(fun(1, 1), fun(n, m), fun(1, m), fun(n, 1))
if ans > 0 and (n <= a or m <= b):
	print('Poor Inna and pony!')
elif ans == 1000000000:
	print('Poor Inna and pony!')
else:
	print(ans)


(n, m, i, j, a, b) = map(int, input().split())
l = []
if (i - 1) % a == 0 and (j - 1) % b == 0:
	s1 = (i - 1) // a
	s2 = (j - 1) // b
	if (s1 + s2) % 2 == 0:
		l.append(max(s1, s2))
if (i - 1) % a == 0 and (m - j) % b == 0:
	s1 = (i - 1) // a
	s2 = (m - j) // b
	if (s1 + s2) % 2 == 0:
		l.append(max(s1, s2))
if (n - i) % a == 0 and (j - 1) % b == 0:
	s1 = (n - i) // a
	s2 = (j - 1) // b
	if (s1 + s2) % 2 == 0:
		l.append(max(s1, s2))
if (n - i) % a == 0 and (m - j) % b == 0:
	s1 = (n - i) // a
	s2 = (m - j) // b
	if (s1 + s2) % 2 == 0:
		l.append(max(s1, s2))
if (i, j) == (0, 0) or (i, j) == (n, m) or (i, j) == (n, 1) or ((i, j) == (1, m)):
	print(0)
elif i + a > n and i - a < 1:
	print('Poor Inna and pony!')
elif j + b > m and j - b < 1:
	print('Poor Inna and pony!')
elif len(l) == 0:
	print('Poor Inna and pony!')
else:
	print(min(l))


import sys
input = sys.stdin.readline
(n, m, i, j, a, b) = map(int, input().split())
MIN = 10 ** 18

def calc(u, v):
	global MIN
	if (u - i) % a == 0 and (v - j) % b == 0:
		x = abs(u - i) // a
		y = abs(v - j) // b
		if x % 2 == y % 2 and 1 + a <= n and (1 + b <= m):
			MIN = min(MIN, max(x, y))
		if x == y == 0:
			MIN = 0
calc(1, 1)
calc(1, m)
calc(n, 1)
calc(n, m)
if MIN == 10 ** 18:
	print('Poor Inna and pony!')
else:
	print(MIN)


(n, m, i, j, a, b) = map(int, input().split())
ans = 100000000000
if (i, j) == (n, 1) or (i, j) == (1, 1) or (i, j) == (1, m) or ((i, j) == (n, m)):
	print(0)
else:
	hor = i - 1
	ver = j - 1
	if hor % a == 0 and ver % b == 0:
		x = hor // a
		y = ver // b
		if x % 2 == y % 2 and a <= n - 1 and (b <= m - 1):
			ans = min(ans, max(x, y))
	hor = abs(i - n)
	ver = abs(j - m)
	if hor % a == 0 and ver % b == 0:
		x = hor // a
		y = ver // b
		if x % 2 == y % 2 and a <= n - 1 and (b <= m - 1):
			ans = min(ans, max(x, y))
	hor = i - 1
	ver = abs(j - m)
	if hor % a == 0 and ver % b == 0:
		x = hor // a
		y = ver // b
		if x % 2 == y % 2 and a <= n - 1 and (b <= m - 1):
			ans = min(ans, max(x, y))
	hor = abs(n - i)
	ver = j - 1
	if hor % a == 0 and ver % b == 0:
		x = hor // a
		y = ver // b
		if x % 2 == y % 2 and a <= n - 1 and (b <= m - 1):
			ans = min(ans, max(x, y))
	if ans != 100000000000:
		print(ans)
	else:
		print('Poor Inna and pony!')


bot = True
(n, m, i, j, a, b) = map(int, input().split())
(x, y, t) = ([i - 1, n - i], [j - 1, m - j], [])
if all((i < a for i in x)) or all((j < b for j in y)):
	if 0 in x and 0 in y:
		t = [0]
else:
	u = [d // a for d in x if d % a == 0]
	v = [d // b for d in y if d % b == 0]
	t = [max(i, j) for i in u for j in v if (i + j) % 2 == 0]
print(min(t) if t else 'Poor Inna and pony!')


(n, m, i, j, a, b) = (0, 0, 0, 0, 0, 0)
inf = 1000000000

def check(x, y):
	if x == i and y == j:
		return 0
	if i - a < 1 and i + a > n:
		return inf
	if j - b < 1 and j + b > m:
		return inf
	if abs(x - i) % a != 0:
		return inf
	if abs(y - j) % b != 0:
		return inf
	cost = 0
	cost = max(abs(x - i) // a, abs(y - j) // b)
	tmp = abs(x - i) // a + abs(y - j) // b
	if tmp & 1:
		return inf
	return cost

def solve():
	ans = inf
	ans = min(check(1, m), check(n, 1))
	ans = min(ans, min(check(n, m), check(1, 1)))
	if ans == inf:
		return 'Poor Inna and pony!'
	else:
		return ans
(n, m, i, j, a, b) = map(int, input().split())
print(solve())


lst = list(map(int, input().strip().split()))
n = lst[0]
m = lst[1]
corners = [[1, 1], [1, m], [n, m], [n, 1]]
point = [lst[2], lst[3]]
a = lst[4]
b = lst[5]
corners2 = []
for corner in corners:
	if (corner[0] - point[0]) % a == 0 and (corner[1] - point[1]) % b == 0 and (corner not in corners2):
		corners2.append(corner)
corners.clear()
if not corners2:
	print('Poor Inna and pony!')
else:
	for i in range(len(corners2)):
		corner = corners2[i]
		if point in corners2:
			corners.append(0)
		else:
			move1 = abs(corner[0] - point[0]) // a
			move2 = abs(corner[1] - point[1]) // b
			if (move1 - move2) % 2 == 0 and (point[0] + a in range(1, n + 1) or point[0] - a in range(1, n + 1)) and (point[1] + b in range(1, m + 1) or point[1] - b in range(1, m + 1)):
				corners.append(max(move1, move2))
	if not corners:
		print('Poor Inna and pony!')
	else:
		print(min(corners))


(x1, y1, x3, y3, x2, y2) = input().split()
x1 = int(x1)
x2 = int(x2)
x3 = int(x3)
y1 = int(y1)
y2 = int(y2)
y3 = int(y3)
mi = x1 * y1
if (x3 - 1) % x2 == 0 and (y3 - 1) % y2 == 0 and ((x3 - 1) / x2 % 2 == (y3 - 1) / y2 % 2):
	mi = min(mi, max((x3 - 1) / x2, (y3 - 1) / y2))
if (x3 - 1) % x2 == 0 and (y1 - y3) % y2 == 0 and ((x3 - 1) / x2 % 2 == (y1 - y3) / y2 % 2):
	mi = min(mi, max((x3 - 1) / x2, (y1 - y3) / y2))
if (x1 - x3) % x2 == 0 and (y3 - 1) % y2 == 0 and ((x1 - x3) / x2 % 2 == (y3 - 1) / y2 % 2):
	mi = min(mi, max((x1 - x3) / x2, (y3 - 1) / y2))
if (x1 - x3) % x2 == 0 and (y1 - y3) % y2 == 0 and ((x1 - x3) / x2 % 2 == (y1 - y3) / y2 % 2):
	mi = min(mi, max((x1 - x3) / x2, (y1 - y3) / y2))
if mi == x1 * y1 or ((x2 >= x3 and x2 > x1 - x3) and mi != 0) or ((y2 >= y3 and y2 > y1 - y3) and mi != 0):
	print('Poor Inna and pony!')
else:
	print(int(mi))


def inna_and_pink_pony(n_int, m_int, i_int, j_int, a_int, b_int) -> str:
	corner_1 = (1, 1)
	corner_2 = (1, m_int)
	corner_3 = (n_int, 1)
	corner_4 = (n_int, m_int)
	corners = [corner_1, corner_2, corner_3, corner_4]
	c = max(n_int, m_int)
	for each in corners:
		if abs(each[0] - i_int) % a_int != 0 or abs(each[1] - j_int) % b_int != 0:
			continue
		horizontal_moves = abs(each[0] - i_int) // a_int
		vertical_moves = abs(each[1] - j_int) // b_int
		if horizontal_moves % 2 != vertical_moves % 2:
			continue
		each_move = max(horizontal_moves, vertical_moves)
		if a_int > max(i_int - 1, n_int - i) and abs(each[1] - j_int) > 0 or (b_int > max(j - 1, m - j) and abs(each[0] - i_int) > 0):
			break
		c = min(c, each_move)
	if c < max(n_int, m_int):
		return c
	else:
		return 'Poor Inna and pony!'
(n, m, i, j, a, b) = list(map(int, input().split()))
print(inna_and_pink_pony(n, m, i, j, a, b))


(n, m, i, j, a, b) = map(int, input().split())
cor = [[1, 1], [1, m], [n, 1], [n, m]]
flag = 0
dis = 100000001
for k in cor:
	x1 = abs(k[0] - i)
	y1 = abs(k[1] - j)
	if x1 % a != 0 or y1 % b != 0:
		continue
	x1 = int(x1 / a)
	y1 = int(y1 / b)
	if x1 % 2 != y1 % 2:
		continue
	else:
		flag = 1
		temp = max(x1, y1)
		if temp < dis:
			dis = temp
if [i, j] in cor:
	print(0)
elif flag == 0 or n <= a or m <= b:
	print('Poor Inna and pony!')
else:
	print(dis)


inf = 1000 * 1000 * 1000

def calc(x, y):
	if x % a != 0 or y % b != 0:
		return inf
	s = x / a + y / b
	if s % 2 == 1:
		return inf
	return max(x / a, y / b)
(n, m, x, y, a, b) = map(int, input().split())
if x == 1 and y == 1 or (x == 1 and y == m) or (x == n and y == 1) or (x == n and y == m):
	print(0)
	exit(0)
if x - a <= 0 and x + a > n or (y + b > m and y - b <= 0):
	print('Poor Inna and pony!')
	exit(0)
ans = calc(x - 1, y - 1)
ans = min(ans, calc(x - 1, m - y))
ans = min(ans, calc(n - x, y - 1))
ans = min(ans, calc(n - x, m - y))
ans = int(ans)
print(ans if ans != inf else 'Poor Inna and pony!')


(n, m, i, j, a, b) = map(int, input().split())
path1 = path2 = path3 = path4 = 10000000000
temp1 = temp2 = temp3 = temp4 = 0
if int((i - 1) / a) == (i - 1) / a and int((j - 1) / b) == (j - 1) / b:
	path1 = (i - 1) / a
	path_1 = (j - 1) / b
	if path1 % 2 == 0 and path_1 % 2 == 0:
		path1 = max(path1, path_1)
		temp1 = 1
	elif path1 % 2 != 0 and path_1 % 2 != 0:
		path1 = max(path1, path_1)
		temp1 = 1
	else:
		path1 = 100000000000
		temp1 = 0
if int((i - 1) / a) == (i - 1) / a and int((m - j) / b) == (m - j) / b:
	temp = 1
	path2 = (i - 1) / a
	path_2 = (m - j) / b
	if path2 % 2 == 0 and path_2 % 2 == 0:
		path2 = max(path2, path_2)
		temp2 = 1
	elif path2 % 2 != 0 and path_2 % 2 != 0:
		path2 = max(path2, path_2)
		temp2 = 1
	else:
		path2 = 100000000000
		temp2 = 0
if int((n - i) / a) == (n - i) / a and int((j - 1) / b) == (j - 1) / b:
	temp = 1
	path3 = (n - i) / a
	path_3 = (j - 1) / b
	if path3 % 2 == 0 and path_3 % 2 == 0:
		path3 = max(path3, path_3)
		temp3 = 1
	elif path3 % 2 != 0 and path_3 % 2 != 0:
		path3 = max(path3, path_3)
		temp3 = 1
	else:
		path3 = 10000000000000
		temp3 = 0
if int((n - i) / a) == (n - i) / a and int((m - j) / b) == (m - j) / b:
	temp = 1
	path4 = (n - i) / a
	path_4 = (m - j) / b
	if path4 % 2 == 0 and path_4 % 2 == 0:
		path4 = max(path4, path_4)
		temp4 = 1
	elif path4 % 2 != 0 and path_4 % 2 != 0:
		path4 = max(path4, path_4)
		temp4 = 1
	else:
		path4 = 1900000000000
		temp4 = 0
mini = min(path1, path2, path3, path4)
if (i + a > n and i - a < 1 or (j + b > m and j - b < 1)) and mini != 0:
	print('Poor Inna and pony!')
elif temp1 == 0 and temp2 == 0 and (temp3 == 0) and (temp4 == 0):
	print('Poor Inna and pony!')
else:
	print(int(min(path1, path2, path3, path4)))


(n, m, i, j, a, b) = list(map(int, input().split()))

def solve(x, y):
	(dx, dy) = (abs(x - i), abs(y - j))
	if dx % a or dy % b:
		return -1
	elif dx // a % 2 != dy // b % 2:
		return -1
	elif dx == 0 and dy == 0:
		return 0
	elif i - a < 1 and i + a > n or (j - b < 1 and j + b > m):
		return -1
	else:
		return max(dx // a, dy // b)
res = -1
for k in ((1, m), (n, 1), (n, m), (1, 1)):
	curr = solve(k[0], k[1])
	if curr != -1 and (res == -1 or curr < res):
		res = curr
print(res if res != -1 else 'Poor Inna and pony!')


(n, m, ix, jy, a, b) = list(map(int, input().rstrip().split()))

def check(x, y):
	ax = abs(x - ix)
	by = abs(y - jy)
	q1 = ax / a
	q2 = by / b
	if q2 == int(q2) and q1 == int(q1):
		maxm = max(int(q1), int(q2))
		minm = min(int(q1), int(q2))
		if maxm % 2 == 0 and minm % 2 == 0 or (maxm % 2 == 1 and minm % 2 == 1):
			return maxm
	return -1
li = [[1, 1], [1, m], [n, 1], [n, m]]
ans = []
if [ix, jy] in li:
	ans += [0]
elif max(abs(m - jy), abs(jy - 1)) >= b and max(abs(n - ix), abs(ix - 1)) >= a:
	for i in li:
		get = check(i[0], i[1])
		if get != -1:
			ans += [get]
if len(ans) == 0:
	print('Poor Inna and pony!')
else:
	print(min(ans))


import sys

def solve():
	(n, m, i, j, a, b) = map(int, input().split())
	i -= 1
	j -= 1
	n -= 1
	m -= 1
	if (i == 0 or i == n) and (j == 0 or j == m):
		return 0
	if i - a < 0 and i + a > n or (j - b < 0 and j + b > m):
		return 'Poor Inna and pony!'
	uprow = i
	downrows = n - i
	leftcol = j
	rightcol = m - j
	ans = 10000000000
	if uprow % a == 0 and leftcol % b == 0 and (abs(uprow // a - leftcol // b) % 2 == 0):
		ans = min(ans, max(uprow // a, leftcol // b))
	if uprow % a == 0 and rightcol % b == 0 and (abs(uprow // a - rightcol // b) % 2 == 0):
		ans = min(ans, max(uprow // a, rightcol // b))
	if downrows % a == 0 and leftcol % b == 0 and (abs(downrows // a - leftcol // b) % 2 == 0):
		ans = min(ans, max(downrows // a, leftcol // b))
	if downrows % a == 0 and rightcol % b == 0 and (abs(downrows // a - rightcol // b) % 2 == 0):
		ans = min(ans, max(downrows // a, rightcol // b))
	return ans if ans != 10000000000 else 'Poor Inna and pony!'
if sys.hexversion == 50594544:
	sys.stdin = open('test.txt')
print(solve())


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10 ** 8)
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=2 ** 51, func=lambda a, b: a & b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: math.gcd(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(n, m, i, j, a, b) = map(int, input().split())

def check(x1, y1):
	if abs(y1 - j) % b != 0:
		return -1
	if abs(x1 - i) % a != 0:
		return -1
	z = (y1 - j) // b
	z1 = (x1 - i) // a
	ans = 10 ** 7
	for p in range(2 * max(m, n) + 1):
		q = p - (x1 - i) // a
		if q < 0:
			continue
		if (z + p + q) % 2 == 0:
			p1 = (z + p + q) // 2
			if p1 < 0:
				continue
			if p + q - p1 >= 0:
				q1 = (p + q - z) // 2
				t = 0
				if j + b * (p1 - q1) == y1:
					if p > q:
						if 1 <= i + min(p, 1) * a <= n:
							t += 1
					elif q > p:
						if 1 <= i - min(q, 1) * a <= n:
							t += 1
					elif 1 <= i + min(p, 1) * a <= n or 1 <= i - min(q, 1) * a <= n:
						t += 1
					if p1 > q1:
						if 1 <= j + min(p1, 1) * b <= m:
							t += 1
					elif q1 > p1:
						if 1 <= j - min(q1, 1) * b <= m:
							t += 1
					elif 1 <= j + min(p1, 1) * b <= m or 1 <= j - min(q1, 1) * b <= m:
						t += 1
					if t >= 2:
						ans = min(ans, p + q)
	if ans == 10 ** 7:
		ans = -1
	return ans
an = 10 ** 7
aw = check(n, 1)
if aw != -1:
	an = min(an, aw)
aw = check(1, m)
if aw != -1:
	an = min(an, aw)
aw = check(1, 1)
if aw != -1:
	an = min(an, aw)
aw = check(n, m)
if aw != -1:
	an = min(an, aw)
if an == 10 ** 7:
	print('Poor Inna and pony!')
else:
	print(an)


(n, m, i, j, a, b) = list(map(int, input().split()))

def f(x, y):
	(dx, dy) = (abs(x - i), abs(y - j))
	if dx % a or dy % b:
		return -1
	elif dx // a % 2 != dy // b % 2:
		return -1
	elif dx == 0 and dy == 0:
		return 0
	elif i - a < 1 and i + a > n or (j - b < 1 and j + b > m):
		return -1
	else:
		return max(dx // a, dy // b)
v = -1
for p in ((1, m), (n, 1), (n, m), (1, 1)):
	c = f(*p)
	if c != -1 and (v == -1 or c < v):
		v = c
print(v if v != -1 else 'Poor Inna and pony!')


(n, m, i, j, a, b) = map(int, input().split())
p = [(1, m), (n, 1), (n, m), (1, 1)]
c = 0
mn = 1000000000.0
fnd = False
for c in range(4):
	if abs(p[c][0] - i) % a == 0 and abs(p[c][1] - j) % b == 0 and (((p[c][0] - i) / a + (p[c][1] - j) / b) % 2 == 0):
		a1 = abs(p[c][0] - i) / a
		b1 = abs(p[c][1] - j) / b
		if (1 <= i - a or i + a <= n or max(a1, b1) == 0) and (1 <= j - b or j + b <= m or max(a1, b1) == 0):
			mn = min(mn, int(max(a1, b1)))
			fnd = True
if fnd:
	print(mn)
else:
	print('Poor Inna and pony!')


def main():
	(n, m, x, y, a, b) = map(int, input().split(' '))
	mi = int(1000000000.0)
	for (x2, y2) in [(1, m), (n, 1), (n, m), (1, 1)]:
		if x == x2 and y == y2:
			return 0
		if abs(x2 - x) % a != 0 or abs(y2 - y) % b != 0:
			continue
		o = abs(x2 - x) // a
		p = abs(y2 - y) // b
		if not o % 2 == p % 2:
			continue
		mi = min(mi, max(o, p))
	if int(mi) == int(1000000000.0) or n <= a or m <= b:
		return 'Poor Inna and pony!'
	return int(mi)
print(main())


(n, m, i, j, a, b) = map(int, input().split())
Good = lambda i, a, n, d: None if abs(i - d) % a != 0 else abs(i - d) // a % 2 if abs(i - d) // a >= 1 else 0 if i + a <= n or i - a >= 1 else -1
res = None
(xx, yy) = ([1, n], [1, m])
for dx in xx:
	for dy in yy:
		(tx, ty) = (Good(i, a, n, dx), Good(j, b, m, dy))
		if tx is None or ty is None:
			continue
		if tx == -1 or ty == -1:
			if tx == -1 and abs(j - dy) // b == 0 or (ty == -1 and abs(i - dx) // a == 0):
				res = 0
			continue
		if tx != ty:
			continue
		new_res = max(abs(i - dx) // a, abs(j - dy) // b)
		res = new_res if res is None or res > new_res else res
print('Poor Inna and pony!' if res is None else res)


z = input()
x = z.split(' ')
y = [int(o) for o in x]
n = y[0]
m = y[1]
i = y[2]
j = y[3]
a = y[4]
b = y[5]
l = []
if (n - i) % a == 0 and (m - j) % b == 0:
	if ((n - i) / a - (m - j) / b) % 2 == 0:
		if not (i == n and j != m and (n - 1 < a)) and (not (i != n and j == m and (m - 1 < b))):
			l.append(max((n - i) / a, (m - j) / b))
if (i - 1) % a == 0 and (j - 1) % b == 0:
	if ((i - 1) / a - (j - 1) / b) % 2 == 0:
		if not (i == 1 and j != 1 and (n - 1 < a)) and (not (i != 1 and j == 1 and (m - 1 < b))):
			l.append(max((i - 1) / a, (j - 1) / b))
if (i - 1) % a == 0 and (m - j) % b == 0:
	if ((i - 1) / a - (m - j) / b) % 2 == 0:
		if not (i == 1 and j != m and (n - 1 < a)) and (not (i != 1 and j == m and (m - 1 < b))):
			l.append(max((i - 1) / a, (m - j) / b))
if (n - i) % a == 0 and (j - 1) % b == 0:
	if ((n - i) / a - (j - 1) / b) % 2 == 0:
		if not (i == n and j != 1 and (n - 1 < a)) and (not (i != n and j == 1 and (m - 1 < b))):
			l.append(max((n - i) / a, (j - 1) / b))
if len(l) == 0:
	print('Poor Inna and pony!')
else:
	print(int(min(l)))


3

def readln():
	return list(map(int, input().split()))
(n, m, i, j, a, b) = readln()

def solve(w, h):
	if w == 0 and h == 0:
		return 0
	if w == 0 and a < n and (h % 2 == 0):
		return h
	if h == 0 and b < m and (w % 2 == 0):
		return w
	if w == 0 or h == 0:
		return 1 << 30
	if (w + h) % 2 == 0:
		return max(w, h)
	return 1 << 30
ans = min([1 << 30] + [solve(abs(i - p) // a, abs(j - q) // b) for (p, q) in ((1, 1), (1, m), (n, 1), (n, m)) if abs(i - p) % a == 0 and abs(j - q) % b == 0])
print('Poor Inna and pony!' if ans == 2 ** 30 else ans)


from sys import setrecursionlimit, exit
from math import ceil, floor, acos, pi
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from functools import reduce
import itertools
setrecursionlimit(10 ** 7)
RI = lambda x=' ': list(map(int, input().split(x)))
RS = lambda x=' ': input().rstrip().split(x)
dX = [-1, 1, 0, 0, -1, 1, -1, 1]
dY = [0, 0, -1, 1, 1, -1, -1, 1]
mod = int(1000000000.0 + 7)
eps = 1e-06
MAX = 1000000000.0
(n, m, i, j, a, b) = RI()
min_val = MAX
if (i, j) == (1, 1) or (i, j) == (1, m) or (i, j) == (n, 1) or ((i, j) == (n, m)):
	print(0)
else:
	if (n - i) % a == 0 and (m - j) % b == 0 and (((n - i) // a - (m - j) // b) % 2 == 0) and (a <= n - 1) and (b <= m - 1):
		min_val = min(min_val, max((n - i) // a, (m - j) // b))
	if (i - 1) % a == 0 and (m - j) % b == 0 and (((i - 1) // a - (m - j) // b) % 2 == 0) and (a <= n - 1) and (b <= m - 1):
		min_val = min(min_val, max((i - 1) // a, (m - j) // b))
	if (n - i) % a == 0 and (j - 1) % b == 0 and (((n - i) // a - (j - 1) // b) % 2 == 0) and (a <= n - 1) and (b <= m - 1):
		min_val = min(min_val, max((n - i) // a, (j - 1) // b))
	if (i - 1) % a == 0 and (j - 1) % b == 0 and (((i - 1) // a - (j - 1) // b) % 2 == 0) and (a <= n - 1) and (b <= m - 1):
		min_val = min(min_val, max((i - 1) // a, (j - 1) // b))
	if min_val == MAX:
		print('Poor Inna and pony!')
	else:
		print(min_val)


(n, m, i, j, a, b) = map(int, input().split())
corners = [(1, 1), (1, m), (n, 1), (n, m)]
result = False
ans = -1
for cnt in corners:
	if abs(cnt[0] - i) % a == 0 and abs(cnt[1] - j) % b == 0:
		result = True
		(t1, t2) = (abs(cnt[0] - i) // a, abs(cnt[1] - j) // b)
		if (t1 + t2) % 2 == 0:
			if ans == -1:
				ans = max(abs(cnt[0] - i) // a, abs(cnt[1] - j) // b)
			else:
				ans = min(max(abs(cnt[0] - i) // a, abs(cnt[1] - j) // b), ans)
if ans == 0:
	print(ans)
elif not result or (i + a > n and i - a < 1) or (j + b > m and j - b < 1) or (ans == -1):
	print('Poor Inna and pony!')
else:
	print(ans)


def find(x, y):
	distx = abs(x - i)
	disty = abs(y - j)
	if distx == 0 and disty == 0:
		return 0
	if distx % a != 0 or disty % b != 0:
		return float('inf')
	elif abs(distx // a - disty // b) % 2 != 0:
		return float('inf')
	elif distx == 0 and (x + a > n and x - a < 1):
		return float('inf')
	elif disty == 0 and (y + b > m and y - b < 1):
		return float('inf')
	else:
		return max(distx // a, disty // b)
(n, m, i, j, a, b) = map(int, input().split())
ans = min(find(1, 1), find(1, m), find(n, 1), find(n, m))
print(ans if ans != float('inf') else 'Poor Inna and pony!')


def num_steps(x, y):
	if i == x and j == y:
		return 0
	dx = abs(j - y)
	dy = abs(i - x)
	if dy == 0 and i - a < 1 and (i + a > n) or (dx == 0 and j - b < 1 and (j + b > m)):
		return -1
	if dy % a != 0 or dx % b != 0:
		return -1
	ny = dy // a
	nx = dx // b
	if nx % 2 != ny % 2:
		return -1
	return max(nx, ny)
(n, m, i, j, a, b) = map(int, input().split())
cand = list(filter(lambda x: x >= 0, (num_steps(1, 1), num_steps(n, 1), num_steps(1, m), num_steps(n, m))))
if not cand:
	print('Poor Inna and pony!')
else:
	print(min(cand))


(n, m, i, j, a, b) = map(int, input().split())

def solve(x, y):
	if i - x == 0 and j - y == 0:
		return 0
	if i - a < 1 and i + a > n or (j - b < 1 and j + b > m):
		return -1
	elif not abs(x - i) % a and (not abs(j - y) % b) and (not (abs(x - i) // a + (y - j) // b) % 2):
		return max(abs(x - i) // a, abs(y - j) // b)
	else:
		return -1
vals = [(1, 1), (n, 1), (1, m), (n, m)]
ans = -1
for values in vals:
	res = solve(values[0], values[1])
	if res != -1 and (ans == -1 or res < ans):
		ans = res
if ans == -1:
	print('Poor Inna and pony!')
else:
	print(ans)


(n, m, i, j, a, b) = [int(x) for x in input().split()]
i -= 1
j -= 1
best = float('inf')

def find(x, y, a, b, n, m):
	n = n // a
	m = m // b
	x = x // a
	y = y // b
	total = min(x, y)
	x -= total
	y -= total
	if (y + x) % 2 == 0:
		if y == 0 and m > 1:
			return y + x + total
		elif x == 0 and n > 1:
			return y + x + total
		elif y == 0 and x == 0:
			return y + x + total
		else:
			return float('inf')
	else:
		return float('inf')
if i % a == 0:
	x = i
	if j % b == 0:
		y = j
		best = min(best, find(x, y, a, b, n, m))
	if (m - j - 1) % b == 0:
		y = m - j - 1
		best = min(best, find(x, y, a, b, n, m))
if (n - i - 1) % a == 0:
	x = n - i - 1
	if j % b == 0:
		y = j
		best = min(best, find(x, y, a, b, n, m))
	if (m - j - 1) % b == 0:
		y = m - j - 1
		best = min(best, find(x, y, a, b, n, m))
if best == float('inf'):
	print('Poor Inna and pony!')
else:
	print(best)