You are given a rectangular grid of lattice points from (0, 0) to (n, m) inclusive. You have to choose exactly 4 different points to build a polyline possibly with self-intersections and self-touching. This polyline should be as long as possible.

A polyline defined by points p_1, p_2, p_3, p_4 consists of the line segments p_1 p_2, p_2 p_3, p_3 p_4, and its length is the sum of the lengths of the individual line segments.


-----Input-----

The only line of the input contains two integers n and m (0 ≤ n, m ≤ 1000). It is guaranteed that grid contains at least 4 different points.


-----Output-----

Print 4 lines with two integers per line separated by space — coordinates of points p_1, p_2, p_3, p_4 in order which represent the longest possible polyline.

Judge program compares your answer and jury's answer with 10^{ - 6} precision.


-----Examples-----
Input
1 1

Output
1 1
0 0
1 0
0 1

Input
0 10

Output
0 1
0 10
0 0
0 9

Difficulty: MEDIUM_HARD
Tags: Geometry

Tree algorithms

Complete search

Constructive algorithms
Skill types: Complete search

Starter code:

Solutions:
def main():
	a = input().split()
	for i in range(len(a)):
		a[i] = int(a[i])
	m = a[1]
	n = a[0]
	if n == 0:
		c1 = {(0, 0), (0, 1), (0, m - 1), (0, m)}
	elif m == 0:
		c1 = {(0, 0), (1, 0), (n - 1, 0), (n, 0)}
	else:
		c1 = {(0, 0), (0, 1), (1, 0), (1, 1), (0, m - 1), (0, m), (1, m - 1), (1, m), (n - 1, m - 1), (n - 1, m), (n, m - 1), (n, m), (n - 1, 0), (n, 0), (n - 1, 1), (n, 1)}
	maximum = 0
	for i in c1:
		for j in c1 - {i}:
			for k in c1 - {i, j}:
				for l in c1 - {i, j, k}:
					maximum = max(polyline(i, j, k, l), maximum)
	for i in c1:
		for j in c1 - {i}:
			for k in c1 - {i, j}:
				for l in c1 - {i, j, k}:
					if maximum == polyline(i, j, k, l):
						print(l[0], l[1])
						print(k[0], k[1])
						print(j[0], j[1])
						print(i[0], i[1])
						return

def dis(i, j):
	return ((i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) ** 0.5

def polyline(i, j, k, l):
	return dis(i, j) + dis(j, k) + dis(k, l)
main()


import math
(n, m) = map(int, input().split())

def check(a):
	return 0 <= a[0] <= n and 0 <= a[1] <= m

def dist(a, b):
	if not check(a) or not check(b):
		return -1324345543
	return math.hypot(0.0 + a[0] - b[0], a[1] - b[1] + 0.0)
points = [(0, 0), (n, m), (0, 1), (1, 0), (0, m), (n, 0), (n, 1), (m, 1), (n - 1, m - 1), (n - 2, m - 1), (0, m - 1), (n - 1, 0), (n - 2, 0), (n - 1, m - 2), (1, 1), (2, 2), (n - 2, m - 2), (2, 1), (1, 2), (0, 0), (0, 1), (1, 0), (1, 1), (0, m), (0, m - 1), (0, m - 2), (1, m), (2, m), (n, 0), (n, 1), (n, 2), (n - 1, 0), (n - 1, 1), (n - 2, 1)]
points = list(set([q if check(q) else (0, 0) for q in points]))
ans = 0
anns = []
for a in points:
	for b in points:
		for c in points:
			for d in points:
				if len({a, b, c, d}) == 4:
					les = dist(a, b) + dist(b, c) + dist(c, d)
					if les > ans:
						ans = les
						anns = [a, b, c, d]
for i in anns:
	print(*i)


(n, m) = [int(s) for s in input().split()]
p = [(i, j) for i in range(4) for j in range(4) if i + j < 4 and i <= n and (j <= m)]
q = [(n - i, m - j) for (i, j) in p]
p = set(p + q)
from math import sqrt

def dist(a, b):
	return sqrt((a[0] - b[0]) ** 2.0 + (a[1] - b[1]) ** 2.0)

def dp(a, b, c, d):
	return dist(a, b) + dist(b, c) + dist(c, d)
t = -1
r = None
for a in p:
	for b in p:
		if b == a:
			continue
		for c in p:
			if c == a or c == b:
				continue
			for d in p:
				if d == a or d == b or d == c:
					continue
				f = dp(a, b, c, d)
				if f > t:
					r = (a, b, c, d)
					t = f
for (i, j) in r:
	print(i, j)


(n, m) = list(map(int, input().split()))
data = [[0, i] for i in range(min(m + 1, 5))] + [[j, 0] for j in range(min(n + 1, 5))] + [[n, k] for k in range(max(0, m - 5), m + 1)] + [[l, m] for l in range(max(n - 5, 0), n + 1)]
(a, b, c, d) = (0, 0, 0, 0)
r = -1

def check(a, b, c, d):
	return ((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) ** 0.5 + ((b[0] - c[0]) ** 2 + (b[1] - c[1]) ** 2) ** 0.5 + ((c[0] - d[0]) ** 2 + (c[1] - d[1]) ** 2) ** 0.5
for i in range(len(data)):
	for j in range(len(data)):
		for k in range(len(data)):
			for l in range(len(data)):
				if not (data[i] != data[k] and data[j] != data[l] and (data[i] != data[j]) and (data[i] != data[l]) and (data[j] != data[k]) and (data[k] != data[l])):
					continue
				if r < check(data[i], data[j], data[k], data[l]):
					r = check(data[i], data[j], data[k], data[l])
					(a, b, c, d) = (data[i], data[j], data[k], data[l])
print(a[0], a[1])
print(b[0], b[1])
print(c[0], c[1])
print(d[0], d[1])


from math import *

def main():
	(n, m) = [int(x) for x in input().split()]
	if n == 0:
		print(0, 1)
		print(0, m)
		print(0, 0)
		print(0, m - 1)
	elif m == 0:
		print(1, 0)
		print(n, 0)
		print(0, 0)
		print(n - 1, 0)
	else:
		r1 = 2 * sqrt(n * n + m * m) + max(n, m)
		r2 = 2 * sqrt((n - 1) * (n - 1) + m * m) + sqrt(n * n + m * m)
		r3 = 2 * sqrt((m - 1) * (m - 1) + n * n) + sqrt(n * n + m * m)
		if r3 > max(r1, r2):
			print(n, 1)
			print(0, m)
			print(n, 0)
			print(0, m - 1)
		elif r2 > r1:
			print(n - 1, m)
			print(0, 0)
			print(n, m)
			print(1, 0)
		else:
			print(0, 0)
			print(n, m)
			if n < m:
				print(n, 0)
				print(0, m)
			else:
				print(0, m)
				print(n, 0)
main()


inp = input().split()
x = int(inp[0])
y = int(inp[1])

def distance(points):
	d = 0
	for i in range(len(points) - 1):
		p1 = points[i]
		p2 = points[i + 1]
		d += ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5
	return d
res = None
poss = []
if x != 0 and y != 0:
	poss.append([(0, 1), (x, y), (0, 0), (x, y - 1)])
	poss.append([(1, 0), (x, y), (0, 0), (x - 1, y)])
	poss.append([(0, 0), (x, y), (x, 0), (0, y)])
	poss.append([(0, 0), (x, y), (0, y), (x, 0)])
	d1 = distance(poss[0])
	d2 = distance(poss[1])
	d3 = distance(poss[2])
	d4 = distance(poss[3])
	if d1 >= d2 and d1 >= d3 and (d1 >= d4):
		res = poss[0]
	elif d2 >= d1 and d2 >= d3 and (d2 >= d4):
		res = poss[1]
	elif d3 >= d1 and d3 >= d2 and (d3 >= d4):
		res = poss[2]
	else:
		res = poss[3]
elif x == 0 and y != 0:
	res = [(0, 1), (0, y), (0, 0), (0, y - 1)]
elif x != 0 and y == 0:
	res = [(1, 0), (x, 0), (0, 0), (x - 1, 0)]
else:
	res = [(0, 0), (0, 0), (0, 0), (0, 0)]
for i in res:
	print(i[0], i[1])


(n, m) = map(int, input().split())
v = set()
R = (0, 1)

def f(a, b):
	if 0 <= a <= n and 0 <= b <= m:
		v.add((a, b))
g = lambda x: sum(((x[0][i] - x[1][i]) ** 2 for i in R)) ** 0.5 + g(x[1:]) if len(x) > 1 else 0
r = [[0, 0]] * 4
for i in R:
	for j in R:
		f(i, j)
		f(n - i, m - j)
		f(i, m - j)
		f(n - i, j)
for a in v:
	for b in v:
		for c in v:
			for d in v:
				if len(set([a, b, c, d])) == 4 and g(r) < g([a, b, c, d]):
					r = (a, b, c, d)
for (a, b) in r:
	print(a, b)


(n, m) = map(int, input().split())
if n == 0:
	print('0 1\n0 ' + str(m) + '\n0 0\n0 ' + str(m - 1))
elif m == 0:
	print('1 0\n' + str(n) + ' 0\n0 0\n' + str(n - 1) + ' 0')
elif n < m:
	if (n ** 2 + m ** 2) ** 0.5 + m < 2 * ((n - 1) ** 2 + m ** 2) ** 0.5:
		print(str(n - 1) + ' ' + str(m) + '\n0 0\n' + str(n) + ' ' + str(m) + '\n1 0')
	else:
		print('0 0\n' + str(n) + ' ' + str(m) + '\n' + str(n) + ' 0\n0 ' + str(m))
elif (n ** 2 + m ** 2) ** 0.5 + n < 2 * (n ** 2 + (m - 1) ** 2) ** 0.5:
	print(str(n) + ' ' + str(m - 1) + '\n0 0\n' + str(n) + ' ' + str(m) + '\n0 1')
else:
	print('0 0\n' + str(n) + ' ' + str(m) + '\n0 ' + str(m) + '\n' + str(n) + ' 0')


def dist(v):
	ans = 0
	for i in range(4):
		for j in range(4):
			if i != j and v[i] == v[j]:
				return 0
	for i in range(3):
		ans += (v[i][0] - v[i + 1][0]) ** 2 + (v[i][1] - v[i + 1][1]) ** 2
	return ans
now = [0] * 4
(n, m) = list(map(int, input().split()))
best = 0
if n == 0:
	print(0, 1)
	print(0, m)
	print(0, 0)
	print(0, m - 1)
elif m == 0:
	print(1, 0)
	print(n, 0)
	print(0, 0)
	print(n - 1, 0)
else:
	v = [[(0, 0), (n, m), (0, m), (n, 0)], [(0, 0), (n, m), (n, 0), (0, m)], [(0, 0), (n, m), (0, 1), (n, m - 1)], [(0, 0), (n, m), (1, 0), (n - 1, m)], [(0, 0), (n, m), (0, 1), (n - 1, m)], [(0, 0), (n, m), (1, 0), (n, m - 1)], [(0, 1), (n, m), (0, 0), (n, m - 1)], [(1, 0), (n, m), (0, 0), (n - 1, m)], [(0, 1), (n, m), (0, 0), (n - 1, m)], [(1, 0), (n, m), (0, 0), (n, m - 1)], [(0, 1), (n, m - 1), (0, 0), (n, m)], [(1, 0), (n - 1, m), (0, 0), (n, m)], [(0, 1), (n - 1, m), (0, 0), (n, m)], [(1, 0), (n, m - 1), (0, 0), (n, m)], [(0, 1), (n, m - 1), (1, 0), (n, m)], [(1, 0), (n - 1, m), (0, 1), (n, m)], [(0, 1), (n - 1, m), (1, 0), (n, m)], [(1, 0), (n, m - 1), (0, 1), (n, m)], [(0, 1), (n, m - 1), (1, 0), (n - 1, m)], [(1, 0), (n - 1, m), (0, 1), (n, m - 1)], [(0, 1), (n - 1, m), (1, 0), (n, m - 1)], [(1, 0), (n, m - 1), (0, 1), (n - 1, m)], [(0, 0), (n, m - 1), (0, 1), (n, m)], [(0, 0), (n - 1, m), (1, 0), (n, m)], [(0, 0), (n - 1, m), (0, 1), (n, m)], [(0, 0), (n, m - 1), (1, 0), (n, m)]]
	for i in range(len(v)):
		now = dist(v[i])
		if now > best:
			best = now
			ans = i
	for i in range(4):
		print(v[ans][i][0], v[ans][i][1])


(n, m) = list(map(int, input().split()))
a = ([[0, 1], [0, m], [0, 0], [0, m - 1]], [[1, 0], [n, 0], [0, 0], [n - 1, 0]], [[0, 1], [n, m], [0, 0], [n, m - 1]], [[1, 0], [n, m], [0, 0], [n - 1, m]], [[0, 0], [n, m], [0, m], [n, 0]], [[0, 0], [n, m], [n, 0], [0, m]])
for x in max(a, key=lambda a: (len(set(map(tuple, a))) == len(a)) * all(map(lambda x: min(x[1], x[0]) >= 0, a)) * sum((((a[i][0] - a[i + 1][0]) ** 2 + (a[i][1] - a[i + 1][1]) ** 2) ** 0.5 for i in range(len(a) - 1)))):
	print(*x)
print()


(n, m) = map(int, input().split())
if n == 0:
	print(0, 1)
	print(0, m)
	print(0, 0)
	print(0, m - 1)
elif m == 0:
	print(1, 0)
	print(n, 0)
	print(0, 0)
	print(n - 1, 0)
elif m >= n:
	if 2 * (m ** 2 + (n - 1) ** 2) ** 0.5 >= m + (m ** 2 + n ** 2) ** 0.5:
		print(1, 0)
		print(n, m)
		print(0, 0)
		print(n - 1, m)
	else:
		print(n, m)
		print(0, 0)
		print(0, m)
		print(n, 0)
elif 2 * ((m - 1) ** 2 + n ** 2) ** 0.5 >= n + (m ** 2 + n ** 2) ** 0.5:
	print(0, 1)
	print(n, m)
	print(0, 0)
	print(n, m - 1)
else:
	print(n, m)
	print(0, 0)
	print(n, 0)
	print(0, m)


import math
(x, y) = (int(a) for a in input().split(' '))
ans = []
if x == 0:
	ans.append((0, 1))
	ans.append((0, y))
	ans.append((0, 0))
	ans.append((0, y - 1))
elif y == 0:
	ans.append((1, 0))
	ans.append((x, 0))
	ans.append((0, 0))
	ans.append((x - 1, 0))
elif x > y:
	if y < 2 or math.sqrt(x ** 2 + y ** 2) + x > 2 * math.sqrt(x ** 2 + (y - 1) ** 2):
		ans.append((0, 0))
		ans.append((x, y))
		ans.append((0, y))
		ans.append((x, 0))
	else:
		ans.append((0, 1))
		ans.append((x, y))
		ans.append((0, 0))
		ans.append((x, y - 1))
elif x < 2 or math.sqrt(x ** 2 + y ** 2) + y > math.sqrt(y ** 2 + (x - 1) ** 2) + math.sqrt(y ** 2 + (x - 1) ** 2):
	ans.append((0, 0))
	ans.append((x, y))
	ans.append((x, 0))
	ans.append((0, y))
else:
	ans.append((1, 0))
	ans.append((x, y))
	ans.append((0, 0))
	ans.append((x - 1, y))
for a in ans:
	print(str(a[0]) + ' ' + str(a[1]))


import math
(a, b) = map(int, input().split(' '))
if a == 0:
	print(0, ' ', 1)
	print(0, ' ', b)
	print(0, ' ', 0)
	print(0, ' ', b - 1)
elif b == 0:
	print(1, ' ', 0)
	print(a, ' ', 0)
	print(0, ' ', 0)
	print(a - 1, ' ', 0)
elif a >= b:
	if math.sqrt(a ** 2 + b ** 2) + a > 2 * math.sqrt(a ** 2 + (b - 1) ** 2):
		print(0, ' ', 0)
		print(a, ' ', b)
		print(0, ' ', b)
		print(a, ' ', 0)
	else:
		print(0, ' ', 1)
		print(a, ' ', b)
		print(0, ' ', 0)
		print(a, ' ', b - 1)
elif math.sqrt(a ** 2 + b ** 2) + b > 2 * math.sqrt((a - 1) ** 2 + b ** 2):
	print(0, ' ', 0)
	print(a, ' ', b)
	print(a, ' ', 0)
	print(0, ' ', b)
else:
	print(1, ' ', 0)
	print(a, ' ', b)
	print(0, ' ', 0)
	print(a - 1, ' ', b)


def dist(x1, y1, x2, y2):
	return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5

def ddist(a):
	res = 0.0
	for i in range(3):
		res += dist(a[i][0], a[i][1], a[i + 1][0], a[i + 1][1])
	return res
(n, m) = map(int, input().split())
if n == 0:
	print('0 1')
	print('0', m)
	print(0, 0)
	print(0, m - 1)
elif m == 0:
	print(1, 0)
	print(n, 0)
	print(0, 0)
	print(n - 1, 0)
else:
	aa = [[[n - 1, m], [0, 0], [n, m], [1, 0]], [[0, 1], [n, m], [0, 0], [n, m - 1]], [[0, 0], [n, m], [0, m], [n, 0]], [[n, m], [0, 0], [0, m], [n, 0]]]
	a = max(aa, key=ddist)
	for i in a:
		print(i[0], i[1])


import math
(n, m) = map(int, input().split())
swapped = False
if n > m:
	(n, m) = (m, n)
	swapped = True

def mprint(x, y):
	global swapped
	if swapped:
		print(y, x)
	else:
		print(x, y)

def mlen(pnt):
	res = 0.0
	for i in range(1, 4):
		val = (pnt[i][0] - pnt[i - 1][0]) ** 2
		val += (pnt[i][1] - pnt[i - 1][1]) ** 2
		val = math.sqrt(val)
		res += val
	return res
if n == 0:
	mprint(0, 1)
	mprint(0, m)
	mprint(0, 0)
	mprint(0, m - 1)
else:
	one = [(0, 0), (n, m), (n, 0), (0, m)]
	two = [(1, 0), (n, m), (0, 0), (n - 1, m)]
	if mlen(one) > mlen(two):
		two = one
	for x in two:
		mprint(*x)


(n, m) = map(int, input().split())
if m == 0:
	print(1, 0)
	print(n, 0)
	print(0, 0)
	print(n - 1, 0)
elif n == 0:
	print(0, 1)
	print(0, m)
	print(0, 0)
	print(0, m - 1)
elif n == 1 and m == 1:
	print(1, 1)
	print(0, 0)
	print(1, 0)
	print(0, 1)
elif n == 1:
	print(0, 0)
	print(1, m)
	print(1, 0)
	print(0, m)
elif m == 1:
	print(0, 0)
	print(n, 1)
	print(0, 1)
	print(n, 0)
elif n < m:
	a = pow(n ** 2 + m ** 2, 1 / 2) * 2 + max(n, m)
	b = pow((n - 1) ** 2 + m ** 2, 1 / 2) * 2 + pow(m ** 2 + n ** 2, 1 / 2)
	if b > a:
		print(1, 0)
		print(n, m)
		print(0, 0)
		print(n - 1, m)
	else:
		print(0, 0)
		print(n, m)
		print(n, 0)
		print(0, m)
else:
	a = pow(n ** 2 + m ** 2, 1 / 2) * 2 + max(n, m)
	b = pow((m - 1) ** 2 + n ** 2, 1 / 2) * 2 + pow(m ** 2 + n ** 2, 1 / 2)
	if b > a:
		print(0, 1)
		print(n, m)
		print(0, 0)
		print(n, m - 1)
	else:
		print(0, 0)
		print(n, m)
		print(0, m)
		print(n, 0)


(n, m) = map(int, input().split())
A = set(((i, j) for i in range(2) for j in range(2) if i <= n and j <= m))
A |= set(((n - i, m - j) for i in range(2) for j in range(2) if i <= n and j <= m))
A |= set(((i, m - j) for i in range(2) for j in range(2) if i <= n and j <= m))
A |= set(((n - i, j) for i in range(2) for j in range(2) if i <= n and j <= m))

def dist2(a, b):
	return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2
ans = sorted(((dist2(p1, p2) + dist2(p2, p3) + dist2(p3, p4), p1, p2, p3, p4) for p1 in A for p2 in A for p3 in A for p4 in A if len(set([p1, p2, p3, p4])) == 4))[-1][1:5]
print('\n'.join((str(p[0]) + ' ' + str(p[1]) for p in ans)))


import math
(n, m) = list(map(int, input().split()))
if n == 0:
	print(0, 1)
	print(0, m)
	print(0, 0)
	print(0, m - 1)
elif m == 0:
	print(1, 0)
	print(n, 0)
	print(0, 0)
	print(n - 1, 0)
else:
	l = math.sqrt((n - 1) ** 2 + m ** 2) + math.sqrt(n ** 2 + m ** 2) + math.sqrt(n ** 2 + (m - 1) ** 2)
	l1 = max(m, n) + math.sqrt(n * n + m * m) * 2
	l2 = math.sqrt(n ** 2 + m ** 2) + math.sqrt((n - 1) ** 2 + m ** 2) * 2
	l3 = math.sqrt(n ** 2 + m ** 2) + math.sqrt((m - 1) ** 2 + n ** 2) * 2
	ans = max(l, l1, l2, l3)
	if l == ans:
		print(1, 0)
		print(n, m)
		print(0, 0)
		print(n, m - 1)
	elif l1 == ans:
		if n > m:
			print(n, m)
			print(0, 0)
			print(n, 0)
			print(0, m)
		else:
			print(n, m)
			print(0, 0)
			print(0, m)
			print(n, 0)
	elif l2 == ans:
		print(1, 0)
		print(n, m)
		print(0, 0)
		print(n - 1, m)
	else:
		print(0, 1)
		print(n, m)
		print(0, 0)
		print(n, m - 1)


from math import hypot
(n, m) = list(map(int, input().split()))
f = False
if n > m:
	(n, m) = (m, n)
	f = True
if n == 0:
	for el in [(0, 1), (0, m), (0, 0), (0, m - 1)]:
		print(*(el if not f else reversed(el)))
else:
	p = [[(0, 0), (n, m), (n, 0), (0, m)], [(1, 0), (n, m), (0, 0), (n - 1, m)]]
	t = lambda x: sum((hypot(x[i][0] - x[i - 1][0], x[i][1] - x[i - 1][1]) for i in range(1, 4)))
	for el in max(p, key=t):
		print(*(el if not f else reversed(el)))


from math import *

def d(p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y):
	if len(set([(p1x, p1y), (p2x, p2y), (p3x, p3y), (p4x, p4y)])) != 4:
		return (0, 0)
	dis = sqrt((p2x - p1x) * (p2x - p1x) + (p2y - p1y) * (p2y - p1y)) + sqrt((p3x - p2x) * (p3x - p2x) + (p3y - p2y) * (p3y - p2y)) + sqrt((p4x - p3x) * (p4x - p3x) + (p4y - p3y) * (p4y - p3y))
	return (dis, (p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y))

def bf():
	l = []
	for p1x in range(0, n + 1):
		for p1y in range(0, m + 1):
			for p2x in range(0, n + 1):
				for p2y in range(0, m + 1):
					for p3x in range(0, n + 1):
						for p3y in range(0, m + 1):
							for p4x in range(0, n + 1):
								for p4y in range(0, m + 1):
									if len(set([(p1x, p1y), (p2x, p2y), (p3x, p3y), (p4x, p4y)])) == 4:
										l.append(d(p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y))
	print(list(reversed(sorted(l)))[0])
(n, m) = list(map(int, input().split()))
if m == 0:
	print('%d %d\n%d %d\n%d %d\n%d %d\n' % (1, 0, n, m, 0, 0, n - 1, m))
elif n == 0:
	print('%d %d\n%d %d\n%d %d\n%d %d\n' % (0, 1, n, m, 0, 0, n, m - 1))
else:
	l = []
	l.append(d(n - 1, m, 0, 0, n, m, 0, 1))
	l.append(d(n, m - 1, 0, 0, n, m, 1, 0))
	l.append(d(1, 0, n, m, 0, 0, n - 1, m))
	l.append(d(0, 1, n, m, 0, 0, n, m - 1))
	l.append(d(0, 0, n, m, 0, 1, n - 1, m))
	l.append(d(0, 0, n, m, 1, 0, n, m - 1))
	l.append(d(0, 0, n, m, n, 0, 0, m))
	l.append(d(0, 0, n, m, 0, m, n, 0))
	a = list(reversed(sorted(l)))[0]
	a = a[1]
	for i in range(4):
		print(a[i * 2], a[i * 2 + 1])


def rast(i, j):
	return ((x[i] - x[j]) ** 2 + (y[i] - y[j]) ** 2) ** 0.5

def ij(i, j, h, g):
	if (x[i] != x[j] or y[i] != y[j]) and (x[i] != x[h] or y[i] != y[h]) and (x[i] != x[g] or y[i] != y[g]) and (x[h] != x[j] or y[h] != y[j]) and (x[g] != x[j] or y[g] != y[j]) and (x[g] != x[h] or y[g] != y[h]):
		return True
	return False
(n, m) = map(int, input().split())
if n == 0:
	print(0, 1)
	print(0, m)
	print(0, 0)
	print(0, m - 1)
elif m == 0:
	print(1, 0)
	print(n, 0)
	print(0, 0)
	print(n - 1, 0)
else:
	x = [0, 1, n - 1, n, n, n, n, n - 1, 1, 0, 0, 0]
	y = [0, 0, 0, 0, 1, m - 1, m, m, m, m, m - 1, 1]
	sm = 0
	for i in range(12):
		for j in range(12):
			if x[i] != x[j] or y[i] != y[j]:
				for h in range(12):
					for g in range(12):
						if sm < rast(i, j) + rast(j, h) + rast(g, h) and ij(i, j, h, g):
							sm = rast(i, j) + rast(j, h) + rast(g, h)
							l = [i, j, h, g]
	print(x[l[0]], y[l[0]])
	print(x[l[1]], y[l[1]])
	print(x[l[2]], y[l[2]])
	print(x[l[3]], y[l[3]])