Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1,  - 1)], [( - 1,  - 1), (2,  - 1)], [(2,  - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.

Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).


-----Input-----

The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).


-----Output-----

Print a single integer, showing how many times Valera has to turn.


-----Examples-----
Input
0 0

Output
0

Input
1 0

Output
0

Input
0 1

Output
2

Input
-1 -1

Output
3

Source: codeforces | Difficulty: MEDIUM
Tags: Geometry

Implementation

Complete search


Solutions:
def main():
	(a, b) = list(map(int, input().split()))
	if a == 0 and b == 0:
		return 0
	x = 0
	y = 0
	step = 1
	rep = 0
	direction = 0
	ans = 0
	while x != a or y != b:
		if direction == 0:
			for i in range(step):
				x += 1
				if x == a and y == b:
					return ans
			direction = 1
			ans += 1
		elif direction == 1:
			for i in range(step):
				y += 1
				if x == a and y == b:
					return ans
			direction = 2
			step += 1
			ans += 1
		elif direction == 2:
			for i in range(step):
				x -= 1
				if x == a and y == b:
					return ans
			direction = 3
			ans += 1
		else:
			for i in range(step):
				y -= 1
				if x == a and y == b:
					return ans
			direction = 0
			step += 1
			ans += 1
print(main())


l = input().split(' ')
(x, y) = (int(l[0]), int(l[1]))
if y <= 0 and x >= 1 + y and (x <= 1 - y):
	print(-y * 4)
elif x > 0 and y >= 2 - x and (y <= x):
	print(x * 4 - 3)
elif y > 0 and x >= -y and (x <= y - 1):
	print(y * 4 - 2)
elif x < 0 and y >= x and (y <= -1 - x):
	print(-x * 4 - 1)
else:
	print(0)


def main():
	(x, y) = map(int, input().split())
	(r, t) = (max(abs(x), abs(y)), 0)
	if x == r == 1 - y:
		t = 4
	elif x == r > -y:
		t = 3
	elif y == r > x:
		t = 2
	elif -x == r > y:
		t = 1
	print({(0, 0): 0, (1, 0): 0, (1, 1): 1, (0, 1): 2, (-1, 1): 2, (-1, 0): 3, (-1, -1): 3, (0, -1): 4, (1, -1): 4}[x, y] if r < 2 else r * 4 - t)
main()


(x, y) = map(int, input().split())

def fn(x, y):
	if y == 0 and x == 0:
		return 0
	elif x < 0 and x + y == 0:
		return 4 * abs(y) - 2
	elif x == y:
		if x > 0:
			return 4 * x - 3
		else:
			return 4 * abs(x) - 1
	elif x + y == 1 and x == abs(y) + 1:
		return 4 * abs(y)
	elif abs(x) <= abs(y):
		if y < 0:
			return 4 * abs(y)
		else:
			return 4 * y - 2
	elif abs(x) >= abs(y):
		if x > 0:
			return 4 * x - 3
		else:
			return 4 * abs(x) - 1
print(fn(x, y))


(x, y) = [int(i) for i in input().split()]
if x >= 0:
	if y > x:
		zone = 3
	elif y <= x and y > -x + 1:
		zone = 2
	else:
		zone = 1
elif y >= -x:
	zone = 3
elif y < -x and y >= x:
	zone = 4
else:
	zone = 1
if zone == 1:
	ans = -y * 4
elif zone == 2:
	ans = 1 + (x - 1) * 4
elif zone == 3:
	ans = 2 + (y - 1) * 4
else:
	ans = 3 + -1 * (x + 1) * 4
print(ans)


import sys
import math
from sys import stdin, stdout

def sieve(N):
	primeNumbers = [True] * (N + 1)
	primeNumbers[0] = False
	primeNumbers[1] = False
	i = 2
	while i * i <= N:
		j = i
		if primeNumbers[j]:
			while j * i <= N:
				primeNumbers[j * i] = False
				j += 1
		i += 1
	return primeNumbers

def getPrime(N):
	primes = sieve(N)
	result = []
	for i in range(len(primes)):
		if primes[i]:
			result.append(i)
	return result

def factor(N):
	factors = []
	i = 1
	while i * i <= N:
		if N % i == 0:
			factors.append(i)
			if i != N // i:
				factors.append(N // i)
		i += 1
	return sorted(factors)

def reduceB(b):
	mod = 0
	for i in range(0, len(b)):
		mod = (mod * 10 + ord(b[i])) % 1000000007
	return mod

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

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(input())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def main():
	(x, y) = get_ints_in_variables()
	if x == 0 and y == 0:
		print(0)
	elif y < x and y <= 1 - x:
		print(-4 * y)
	elif y > 1 - x and y <= x:
		print(4 * (x - 1) + 1)
	elif y > x and y >= -x:
		print(4 * (y - 1) + 2)
	else:
		print(4 * (-x - 1) + 3)
main()


(x, y) = list(map(int, input().split()))
if y >= x and y < -x:
	print(-4 * x - 1)
elif y > x and y >= -x:
	print(4 * y - 2)
elif y <= x and y > 1 - x:
	print(4 * x - 3)
elif y < x and y <= 1 - x:
	print(-4 * y)
else:
	print(0)


import os, sys
from io import BytesIO, IOBase

def main():
	(x, y) = map(int, input().split())
	(a, b) = (0, 0)
	tur = 0
	i = 1
	if not x and (not y):
		print(0)
		return
	while 1:
		for _ in range(i):
			a += 1
			if a == x and b == y:
				print(tur)
				return
		tur += 1
		for _ in range(i):
			b += 1
			if a == x and b == y:
				print(tur)
				return
		tur += 1
		i += 1
		for _ in range(i):
			a -= 1
			if a == x and b == y:
				print(tur)
				return
		tur += 1
		for _ in range(i):
			b -= 1
			if a == x and b == y:
				print(tur)
				return
		i += 1
		tur += 1
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')
main()


(m, n) = map(int, input().split())
x = 1
y = 0
count = 0
if 0 <= m <= 1 and n == 0:
	print(0)
else:
	while True:
		(a1, a2) = (x, y)
		(b1, b2) = (a1, abs(a2) + 1)
		if a1 == m and a2 < n <= b2:
			count += 1
			break
		else:
			count += 1
		(c1, c2) = (-b1, b2)
		if c1 <= m < b1 and b2 == n:
			count += 1
			break
		else:
			count += 1
		(d1, d2) = (-b1, -b2)
		if d1 == m and d2 <= n < c2:
			count += 1
			break
		else:
			count += 1
		x = a1 + 1
		y = -(abs(a2) + 1)
		if d1 < m <= x and n == y:
			count += 1
			break
		else:
			count += 1
		if a1 <= m <= b1 and b1 <= n <= b2 or (c1 <= m <= b1 and c2 <= n <= b2) or (c1 <= m <= d1 and d2 <= n <= c2) or (d1 <= m <= x and y <= n <= d2):
			break
	print(count)


p = [(0, 0)]
(a, b) = (0, 0)
c = 1
while c < 202:
	for i in range(c - 1):
		a += 1
		p.append((a, b))
	p.append((a + 1, b, '*'))
	a += 1
	for j in range(c - 1):
		b += 1
		p.append((a, b))
	p.append((a, b + 1, '*'))
	b += 1
	for k in range(c):
		a -= 1
		p.append((a, b))
	p.append((a - 1, b, '*'))
	a -= 1
	for l in range(c):
		b -= 1
		p.append((a, b))
	p.append((a, b - 1, '*'))
	b -= 1
	c += 2
(x, y) = list(map(int, input().split()))
if (x, y) in p:
	z = p.index((x, y))
elif (x, y, '*') in p:
	z = p.index((x, y, '*'))
t = 0
for w in p[:z]:
	if len(w) == 3:
		t += 1
print(t)


def point_on_spiral(x_int, y_int) -> int:
	if x_int == 0 and y_int == 0 or (x_int == 1 and y_int == 0):
		return 0
	count = 0
	(x_prev, y_prev) = (0, 0)
	while True:
		(spiral, count_mod_4) = divmod(count + 4, 4)
		if count_mod_4 == 0:
			(x_curr, y_curr) = (spiral, -(spiral - 1))
		elif count_mod_4 == 1:
			(x_curr, y_curr) = (spiral, spiral)
		elif count_mod_4 == 2:
			(x_curr, y_curr) = (-spiral, spiral)
		else:
			(x_curr, y_curr) = (-spiral, -spiral)
		dist_to_prev = abs(x_int - x_prev) + abs(y_int - y_prev)
		dist_to_curr = abs(x_int - x_curr) + abs(y_int - y_curr)
		dist_prev_curr = abs(x_curr - x_prev) + abs(y_curr - y_prev)
		if dist_to_curr + dist_to_prev == dist_prev_curr:
			return count
		count += 1
		(x_prev, y_prev) = (x_curr, y_curr)
(x, y) = list(map(int, input().split()))
print(point_on_spiral(x, y))


(n, m) = input().split()
n = int(n)
m = int(m)
c = 0
f = not (m == 0 and n == 0)
a = 1
px = 0
py = 0
while f:
	co = 0
	while co < a:
		px += 1
		co += 1
		if n == px and m == py:
			f = False
	if not f:
		break
	c += 1
	co = 0
	while co < a:
		py += 1
		co += 1
		if n == px and m == py:
			f = False
	if not f:
		break
	c += 1
	co = 0
	while co < a + 1:
		px -= 1
		co += 1
		if n == px and m == py:
			f = False
	if not f:
		break
	c += 1
	co = 0
	while co < a + 1:
		py -= 1
		co += 1
		if n == px and m == py:
			f = False
	if not f:
		break
	c += 1
	a += 2
print(c)


(x, y) = map(int, input().split())
sector = 0
flag = 0
if x == 0 and y == 0:
	flag = 1
elif x == 1 and y == 0:
	flag = 2
elif x > 0 and y >= 0 and (x >= y):
	sector = 1
elif x > 0 and y < 0 and (x > 1 - y):
	sector = 1
elif x >= 0 and y > 0 and (x < y):
	sector = 2
elif x < 0 and y > 0 and (-x <= y):
	sector = 2
elif x < 0 and y >= 0 and (-x > y):
	sector = 3
elif x < 0 and y < 0 and (-x >= -y):
	sector = 3
elif x <= 0 and y < 0 and (-y > -x):
	sector = 4
elif x > 0 and y < 0 and (x <= 1 - y):
	sector = 4
if flag == 1 or flag == 2:
	print(0)
elif sector == 1:
	print(4 * (x - 1) + 1)
elif sector == 3:
	print(4 * (-x - 1) + 3)
elif sector == 2:
	print(4 * (y - 1) + 2)
elif sector == 4:
	print(4 * (-y - 1) + 4)


(x, y) = tuple(map(int, input().split()))
if x == 0 and y == 0:
	print(0)
elif x >= 0 and y >= -x + 1 and (y < x):
	if y == -x + 1:
		print(4 * (x - 1))
	else:
		print(4 * (x - 1) + 1)
elif y > 0 and (x == 0 or (x <= abs(y) and x > -abs(y))):
	if y == x:
		print(4 * (abs(y) - 1) + 1)
	else:
		print(4 * (abs(y) - 1) + 2)
elif x <= 0 and y <= -x and (y > x):
	if y == -x:
		print(4 * (abs(x) - 1) + 2)
	else:
		print(4 * (abs(x) - 1) + 1 + 2)
elif y == x:
	print(4 * (abs(y) - 1) + 3)
else:
	print(4 * (abs(y) - 1) + 1 + 3)


import time
(x, y) = map(int, input().split())
movements = [(1, 0), (0, 1), (-1, 0), (0, -1)]
sizeOfSteps = 1
(x_0, y_0) = (0, 0)
(x_1, y_1) = (1, 0)
step = 0
change = 1
while not ((x_0 <= x <= x_1 or x_0 >= x >= x_1) and (y_0 <= y <= y_1 or y_0 >= y >= y_1)):
	step += 1
	move_i = movements[step % 4]
	if change % 2 == 0:
		sizeOfSteps += 1
	change += 1
	(x_0, y_0) = (x_1, y_1)
	x_1 = x_1 + move_i[0] * sizeOfSteps
	y_1 = y_1 + move_i[1] * sizeOfSteps
print(step)


inputs = list(map(int, input().split()))
x = inputs[0]
y = inputs[1]
if y == 0 and 0 <= x <= 1:
	print(0)
else:
	a = max(abs(x), abs(y))
	m = 1 + 4 * (a - 1)
	if x == a and y == -(a - 1):
		print(m - 1)
	if x == a and -(a - 1) < y <= a:
		print(m)
	elif y == a and -a <= x < a:
		print(m + 1)
	elif x == -a and -a <= y < a:
		print(m + 2)
	elif y == -a and -a < x <= a + 1:
		print(m + 3)


(x, y) = list(map(int, input().split()))
c = 0
if y == 0 and (x == 0 or x == 1):
	c = 0
elif abs(x) == abs(y) and x > 0 and (y > 0):
	c = 4 * x - 3
elif abs(x) == abs(y) and x < 0 and (y > 0):
	c = 4 * y - 2
elif abs(x) == abs(y) and x < 0 and (y < 0):
	c = -4 * x - 1
elif abs(x) == abs(y) and x > 0 and (y < 0):
	c = -4 * y
elif x > 0 and y < 0 and (y == -(x - 1)):
	c = -4 * y
elif x > y and abs(x) > abs(y):
	c = 4 * x - 3
elif y > x and abs(y) > abs(x):
	c = 4 * y - 2
elif y > x and abs(x) > abs(y):
	c = -4 * x - 1
elif x > y and abs(x) < abs(y):
	c = -4 * y
print(c)


import sys
import math
import heapq
from collections import defaultdict as dd
from itertools import permutations as pp
from itertools import combinations as cc
from sys import stdin
from functools import cmp_to_key
input = stdin.readline
m = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 5)
(n, m) = map(int, input().split())
(x, y, c) = (0, 0, 0)
if x == n and y == m:
	print(c)
else:
	x += 1
	(f1, f2, f3, f4) = (1, 0, 0, 0)
	(p, q) = (0, 0)
	s = set()
	s.add((1, 0))
	now = 1
	f = 1
	while True:
		if (n, m) in s:
			print(c)
			break
		if f1:
			c += 1
			y += now
			j = 0
			for i in range(now):
				s.add((x, y + j))
				j -= 1
			(f1, f2) = (0, 1)
		elif f2:
			c += 1
			now += 1
			x -= now
			j = 0
			for i in range(now):
				s.add((x + j, y))
				j += 1
			(f2, f3) = (0, 1)
		elif f3:
			c += 1
			y -= now
			j = 0
			for i in range(now):
				s.add((x, y + j))
				j += 1
			(f3, f4) = (0, 1)
		elif f4:
			c += 1
			now += 1
			x += now
			j = 0
			for i in range(now):
				s.add((x + j, y))
				j -= 1
			(f4, f1) = (0, 1)


(x, y) = map(int, input().split())
if x == 1 and y == 0:
	print(0)
	exit()
a = 4 * (x - 1) + 1 if x > 0 else -4 * x - 1 if x < 0 else 0
b = 4 * y - 2 if y > 0 else -4 * y if y < 0 else 0
if abs(x) == abs(y) and y >= x or (y < 0 and abs(x) > abs(y)):
	print(min(a, b))
else:
	print(max(a, b))


(x, y) = list(map(int, input().split()))
(dx, dy) = ([1, 0, -1, 0], [0, 1, 0, -1])

def onl(x1, y1, x2, y2):
	if x1 == x2:
		return x == x1 and y in range(min(y1, y2), max(y1, y2) + 1)
	elif y1 == y2:
		return y == y1 and x in range(min(x1, x2), max(x1, x2) + 1)

def sp(cx, cy, t):
	l = t // 2 + 1
	(nx, ny) = (cx + dx[t % 4] * l, cy + dy[t % 4] * l)
	return t if onl(cx, cy, nx, ny) else sp(nx, ny, t + 1)
print(sp(0, 0, 0))


U = 1
D = -1
(x, y) = map(int, input().split())
k = A = B = 0
while 1:
	(a, b) = (U, B)
	k += 1
	if (A <= x <= a or a <= x <= A) and (B <= y <= b or b <= y <= B):
		break
	A = a
	b = U
	k += 1
	if (A <= x <= a or a <= x <= A) and (B <= y <= b or b <= y <= B):
		break
	B = b
	a = D
	k += 1
	if (A <= x <= a or a <= x <= A) and (B <= y <= b or b <= y <= B):
		break
	A = a
	b = D
	k += 1
	if (A <= x <= a or a <= x <= A) and (B <= y <= b or b <= y <= B):
		break
	(A, B) = (a, b)
	U += 1
	D -= 1
if k < 1:
	k = 1
print(k - 1)


(x, y) = map(int, input().split())
(cur_x, cur_y) = (0, 0)
i = 1
pos = 0
sign = [(1, 0), (0, 1), (-1, 0), (0, -1)]
ans = 0
j = 0
while cur_x != x or cur_y != y:
	if j == i:
		j = 0
		if pos % 2 == 1:
			i += 1
		pos = (pos + 1) % 4
		ans += 1
	cur_x = cur_x + sign[pos][0]
	cur_y = cur_y + sign[pos][1]
	j += 1
print(ans)


(x, y) = input().split()
x = int(x)
y = int(y)
(p, q, z, i, j) = (0, 0, 0, 1, 1)

def GT(n):
	t = 0
	for i in range(1, n + 1):
		if i % 2 != 0:
			t += i
		else:
			t += -i
	return t
while 1:
	(x1, y1) = (max(GT(i), GT(i - 1)), min(GT(i), GT(i - 1)))
	(x2, y2) = (max(GT(j), GT(j - 1)), min(GT(j), GT(j - 1)))
	if z % 2 == 0:
		p = GT(i)
		i += 1
	if z % 2 != 0:
		q = GT(j)
		j += 1
	z += 1
	if x >= y1 and x <= x1 and (y == q):
		break
	if y >= y2 and y <= x2 and (x == p):
		break
if x == 0 and y == 0 or (x == 1 and y == 0):
	print(0)
else:
	print(z)


(x, y) = [int(i) for i in input().split()]
(x1, y1) = (0, 0)
i = 1
direct = 2
counter = 0
br = 0
res = 0
while x1 != x or y1 != y:
	if direct == 1:
		direct = 4
		for j in range(i):
			y1 += 1
			if x1 == x and y1 == y:
				br = 1
				break
	elif direct == 2:
		direct = 1
		for j in range(i):
			x1 += 1
			if x1 == x and y1 == y:
				br = 1
				break
	elif direct == 3:
		direct = 2
		for j in range(i):
			y1 -= 1
			if x1 == x and y1 == y:
				br = 1
				break
	else:
		direct = 3
		for j in range(i):
			x1 -= 1
			if x1 == x and y1 == y:
				br = 1
				break
	if br == 1:
		break
	counter += 1
	if counter % 2 == 0:
		i += 1
	res += 1
print(res)


(x, y) = map(int, input().split())
i = 0
j = 0
n = 1
cnt = 0
while True:
	if n % 2 == 1:
		if cnt % 2 == 0:
			if i <= x and x <= i + n and (j == y):
				break
			else:
				i += n
				cnt += 1
		elif i == x and j <= y and (y <= j + n):
			break
		else:
			j += n
			cnt += 1
			n += 1
	elif cnt % 2 == 0:
		if i - n <= x and x <= i and (j == y):
			break
		else:
			i -= n
			cnt += 1
	elif j - n <= y and y <= j and (i == x):
		break
	else:
		j -= n
		cnt += 1
		n += 1
print(cnt)


from math import *
from collections import *
(x, y) = map(int, input().split())
if x >= y and x + y > 1:
	print(4 * x - 3)
elif x < y and y >= -x:
	print(4 * y - 2)
elif y < -x and y >= x:
	print(4 * -x - 1)
else:
	print(4 * -y)


dest = list(map(int, input().split()))
side_len = 0
pos = [0, 0]
turns = 0
while True:
	side_len += 1
	if dest[1] == pos[1] and pos[0] <= dest[0] <= pos[0] + side_len:
		break
	turns += 1
	pos[0] += side_len
	if dest[0] == pos[0] and pos[1] <= dest[1] <= pos[1] + side_len:
		break
	turns += 1
	pos[1] += side_len
	side_len += 1
	if dest[1] == pos[1] and pos[0] >= dest[0] >= pos[0] - side_len:
		break
	turns += 1
	pos[0] -= side_len
	if dest[0] == pos[0] and pos[1] >= dest[1] >= pos[1] - side_len:
		break
	turns += 1
	pos[1] -= side_len
print(turns)


def spiral():
	(X, Y) = list(map(int, input().split()))
	x = y = 0
	dx = 0
	dy = -1
	turn = 0
	if X == 0 and Y == 0:
		turn = 1
	else:
		while 1:
			if x == X and y == Y:
				break
			if x == y or (x < 0 and x == -y) or (x > 0 and x == 1 - y):
				(dx, dy) = (-dy, dx)
				turn += 1
			(x, y) = (x + dx, y + dy)
	print(turn - 1)
spiral()


import os
import sys
from io import BytesIO, IOBase
mod = 10 ** 9 + 7

def main():
	(x, y) = map(int, input().split())
	(a, b) = (0, 0)
	ans = 0
	z = 1
	if x == a and y == b:
		print(ans)
		return
	while True:
		for i in range(a, z + 1):
			if i == x and y == b:
				print(ans)
				exit()
		a = z
		ans += 1
		for i in range(b, z + 1):
			if i == y and a == x:
				print(ans)
				exit()
		b = z
		ans += 1
		for i in range(a, -a - 1, -1):
			if i == x and y == b:
				print(ans)
				exit()
		a = -a
		ans += 1
		for i in range(b, -b - 1, -1):
			if i == y and a == x:
				print(ans)
				exit()
		b = -b
		ans += 1
		z += 1

def nouse0():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse1():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse2():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
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')

def nouse3():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse4():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse5():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
main()


(x, y) = map(int, input().split())
sx = sy = 0
c = 0
i = j = 1
while 1:
	p = sx
	q = sy
	if i % 2 == 0:
		sy = sy + (-1) ** (j - 1) * j
		j += 1
	else:
		sx = sx + (-1) ** (j - 1) * j
	if (min(p, sx) <= x and x <= max(p, sx)) and (min(q, sy) <= y and y <= max(q, sy)) and (x == p and p == sx or (y == q and q == sy)):
		if x == p and y == q:
			c = c - 1
		break
	i += 1
	c += 1
print(max(c, 0))


(x, y) = map(int, input().split())
x1 = abs(x)
y1 = abs(y)
mx = max(x1, y1)
if x == 1 and y == 0:
	print(0)
elif x == 0 and y == 0:
	print(0)
elif x == 2 and y == -1:
	print(4)
elif x1 == y1:
	if x > 0:
		if y > 0:
			print(4 * x1 - 3)
		elif y < 0:
			print(4 * x1)
	elif x < 0:
		if y > 0:
			print(4 * x1 - 2)
		elif y < 0:
			print(4 * x1 - 1)
elif x1 != y1:
	if x1 == mx:
		if x > 0:
			print(4 * x1 - 3)
		elif x < 0:
			print(4 * x1 - 1)
	elif y1 == mx:
		if y > 0:
			print(4 * y1 - 2)
		elif y < 0:
			print(4 * y1)


from math import floor

def main(xx, yy):
	if xx == 0 and yy == 0:
		return 0
	if xx == 1 and yy == 0:
		return 0
	x = 0
	y = 0
	i = 0
	g = [(1, 0), (0, 1), (-1, 0), (0, -1)]
	while not (x == xx and y == yy):
		prev_x = x
		prev_y = y
		x += int(floor(i / 2) + 1) * g[i % 4][0]
		y += int(floor(i / 2) + 1) * g[i % 4][1]
		i += 1
		if xx >= x and xx <= prev_x and (yy >= y) and (yy <= prev_y):
			return i - 1
		if xx <= x and xx >= prev_x and (yy <= y) and (yy >= prev_y):
			return i - 1
	return i - 1
print(main(*list(map(int, input().split(' ')))))


def nextStep(x, y):
	if x == 0 and y == 0:
		return (1, 0)
	if x == 1 and y == 0:
		return (1, 1)
	if x > 0 and y > 0:
		return (-x, y)
	if x < 0 and y > 0:
		return (x, -y)
	if x < 0 and y < 0:
		return (-x + 1, y)
	if x > 0 and y < 0:
		return (x, -y + 1)

def lie(cor1, cor2, x, y):
	if cor1[0] != cor2[0]:
		return y - cor1[1] == 0 and (x <= max(cor1[0], cor2[0]) and x >= min(cor1[0], cor2[0]))
	else:
		return x - cor1[0] == 0 and (y <= max(cor1[1], cor2[1]) and y >= min(cor1[1], cor2[1]))

def main():
	(a, b) = map(int, input().split())
	cor1 = (0, 0)
	cor2 = nextStep(*cor1)
	turns = 0
	while not lie(cor1, cor2, a, b):
		cor1 = cor2
		cor2 = nextStep(*cor2)
		turns += 1
	print(turns)
main()


3
from math import sin, cos, pi, atan2
(x, y) = tuple(map(int, input().split()))
if (x, y) in ((0, 0), (1, 0)):
	print(0)
elif x >= 1 and -x + 1 < y <= x:
	print(1 + 4 * (x - 1))
elif x < 0 and x <= y < -x:
	print(3 + 4 * (-x - 1))
elif y > 0 and -y <= x < y:
	print(2 + 4 * (y - 1))
else:
	print(-4 * y)


s = input().split()
x = int(s[0])
y = int(s[1])
dirx = [1, 0, -1, 0]
diry = [0, 1, 0, -1]
if x == 0 and y == 0:
	print(0)
else:
	cx = 0
	cy = 0
	dir = 0
	steps = 1
	count = 0
	done = False
	while not done:
		for i in range(steps):
			cx += dirx[dir]
			cy += diry[dir]
			if cx == x and cy == y:
				print(count)
				done = True
				break
		count += 1
		dir = (dir + 1) % 4
		if dir == 0 or dir == 2:
			steps += 1


(a, b) = map(int, input().split())
count = 0
(q, e) = (0, 0)
i = 1
flag = 0
while q != a or e != b:
	for j in range(i):
		q += 1
		if q == a and e == b:
			flag = 1
			break
	if flag:
		break
	count += 1
	for j in range(i):
		e += 1
		if q == a and e == b:
			flag = 1
			break
	i += 1
	if flag:
		break
	count += 1
	for j in range(i):
		q -= 1
		if q == a and e == b:
			flag = 1
			break
	if flag:
		break
	count += 1
	for j in range(i):
		e -= 1
		if q == a and e == b:
			flag = 1
			break
	if flag:
		break
	i += 1
	count += 1
print(count)


(x, y) = map(int, input().split())
if x == y == 0 or (x == 1 and y == 0):
	print(0)
	exit()
dx = [0, -1, 0, 1]
dy = [1, 0, -1, 0]
i = 1
ty = 0
tx = 1
c = 0
while 1:
	i += 0.5
	j = 0
	c += 1
	while j < int(i):
		tx += dx[(c - 1) % 4]
		ty += dy[(c - 1) % 4]
		j += 1
		if x == tx and y == ty:
			print(c)
			exit()


(x, y) = map(int, input().split())
(u, v) = (abs(x), abs(y))
a = max(u, v)
s = 4 * (a - 1)
if u >= v and x > 0 and (y > 1 - a):
	s += 1
elif v >= u and y > 0:
	s += 2
elif u >= v and x < 0:
	s += 3
elif v >= u and y < 0:
	s += 4
print(s if a > 0 else 0)


entry = input().split(' ')
(x, y) = list(map(int, entry))
if abs(y) > abs(x) and y >= 0 or (y > 0 and x == -y):
	(x, y) = (0, y)
	position = 1
elif abs(x) > abs(y) and x <= 0 or (x < 0 and x == y):
	(x, y) = (x, 0)
	position = 2
elif abs(y) > abs(x) and y <= 0 or (x > 0 and (x == -y or y == -x + 1)):
	(x, y) = (0, y)
	position = 3
elif x == 0 and y == 0:
	position = 0
	print(0)
else:
	(x, y) = (x, 0)
	position = 4
if position == 4:
	print(1 + 4 * (x - 1))
elif position == 3:
	print(-4 * y)
elif position == 2:
	print(3 - 4 * (x + 1))
elif position == 1:
	print(2 + 4 * (y - 1))


def detquad(x, y):
	if x > 0 and y > 0:
		return 1
	elif x < 0 and y > 0:
		return 2
	elif x < 0 and y < 0:
		return 3
	elif x > 0 and y < 0:
		return 4

def findprevpoints(x, y):
	if x == 0:
		return (y, y)
	elif y == 0:
		if x > 0:
			return (x, -(x - 1))
		else:
			return (x, -x)
	quad = detquad(x, y)
	if quad == 1:
		if x >= y:
			prevx = x
			prevy = -(x - 1)
		else:
			prevy = y
			prevx = y
	elif quad == 2:
		if y < -x:
			prevx = x
			prevy = -x
		else:
			prevy = y
			prevx = y
	elif quad == 3:
		if -y <= -x:
			prevx = x
			prevy = -x
		else:
			prevx = y
			prevy = y
	elif quad == 4:
		if x <= -y + 1:
			prevx = y
			prevy = y
		else:
			prevx = x
			prevy - (x - 1)
	return (prevx, prevy)
(x, y) = map(int, input().split())
if x == 0 and y == 0 or (x == 1 and y == 0):
	print(0)
elif x == 1 and y == 1:
	print(1)
else:
	spiral = []
	spiral.append((1, 0))
	spiral.append((1, 1))
	for i in range(1, 101):
		spiral.append((-i, i))
		spiral.append((-i, -i))
		spiral.append((i + 1, -i))
		spiral.append((i + 1, i + 1))
	(x1, y1) = findprevpoints(x, y)
	print(spiral.index((x1, y1)) + 1)


(x, y) = map(int, input().split())
start = [0, 0]
turns = 0
flag = 0
while [x, y] != [start[0], start[1]] and flag == 0:
	turns += 1
	if turns % 4 == 1:
		for i in range(2 * (turns // 4 + 1) - 1):
			start[0] += 1
			if [x, y] == [start[0], start[1]]:
				flag = 1
	elif turns % 4 == 2:
		for i in range(2 * (turns // 4 + 1) - 1):
			start[1] += 1
			if [x, y] == [start[0], start[1]]:
				flag = 1
	elif turns % 4 == 3:
		z = -1 * start[0]
		for i in range(start[0], -1 * start[0] - 1, -1):
			if [i, start[1]] == [x, y]:
				flag = 1
		start[0] = -1 * start[0]
	else:
		for i in range(start[1], -1 * start[1] - 1, -1):
			if [start[0], i] == [x, y]:
				flag = 1
		start[1] = -1 * start[1]
print(max(0, turns - 1))


point = list(map(int, input().strip().split()))
i = 0
k = 0
segment = [[0, 0], [1, 0]]
seg_copy = [segment[0], point, segment[1]]
seg_copy.sort(key=lambda j: (j[1 - i], j[i]))
turns = 0
while seg_copy[1] != point:
	turns += 1
	segment[0] = list(segment[1])
	segment[1][1] = segment[1][0]
	if k == 0 or k == 2:
		pass
	elif k == 1:
		segment[1][0] = -segment[1][0]
	elif k == 3:
		segment[1][0] = -segment[1][0] + 1
	k = (k + 1) % 4
	seg_copy = [segment[0], point, segment[1]]
	i = (i + 1) % 2
	seg_copy.sort(key=lambda j: (j[1 - i], j[i]))
print(turns)


(x, y) = list(map(int, input().rstrip().split()))
if x == 0 and y == 0 or (x == 1 and y == 0):
	print(0)
elif x > 0 and y in range(-1 * x + 2, x + 1):
	ans = 1 + 4 * (abs(x) - 1)
	print(ans)
elif y > 0 and x in range(-1 * y, y):
	ans = 1 + 4 * (y - 1)
	print(ans + 1)
elif x < 0 and y in range(x - 1, abs(x)):
	ans = 1 + 4 * (abs(x) - 1)
	print(ans + 2)
else:
	ans = 1 + 4 * (abs(y) - 1)
	print(ans + 3)


import math
(n, m) = map(int, input().split())
if n >= 0 and n <= 1 and (m == 0):
	print(0)
elif n >= 0 and m >= 0:
	if abs(n) >= abs(m):
		print(math.floor(4 * (abs(n) - 1)) + 1) if abs(n) - 1 >= 0 else print(1)
	else:
		print(math.floor(4 * (abs(m) - 1)) + 2) if abs(m) - 1 >= 0 else print(2)
elif n < 0 and m >= 0:
	if abs(n) <= abs(m):
		print(math.floor(4 * (abs(m) - 1)) + 2) if abs(m) - 1 >= 0 else print(2)
	else:
		print(math.floor(4 * (abs(n) - 1)) + 3) if abs(n) - 1 >= 0 else print(3)
elif n <= 0 and m < 0:
	if abs(n) >= abs(m):
		print(math.floor(4 * (abs(n) - 1)) + 3) if abs(n) - 1 >= 0 else print(3)
	else:
		print(math.floor(4 * (abs(m) - 1)) + 4) if abs(m) - 1 >= 0 else print(4)
elif n >= 0 and m <= 0:
	print(math.floor(4 * (abs(m) - 1)) + 4) if abs(m) - 1 >= 0 else print(4)


(x, y) = map(int, input().split())
isCorner = x == y or (-x == y and y > 0) or (y == -x + 1 and x is not 0)
reachX = 0 if x == 0 else 1 + (x - 1) * 4 if x > 0 else 3 + (abs(x) - 1) * 4
reachY = 0 if y == 0 else 2 + (y - 1) * 4 if y > 0 else abs(y) * 4
if isCorner:
	print(min(reachX, reachY))
else:
	print(max(reachX, reachY))


def on(segment, point):
	(start, end) = segment
	if point == start:
		return False
	if point == end:
		return True
	(x1, y1) = start
	(x2, y2) = point
	(x3, y3) = end
	if x1 == x2 and x2 == x3 and (y1 < y2 < y3 or y3 < y2 < y1):
		return True
	if y1 == y2 and y2 == y3 and (x1 < x2 < x3 or x3 < x2 < x1):
		return True
	return False
point = tuple(map(int, input().split()))
(start, end) = ((0, 0), (1, 0))
if point in [start, end]:
	print(0)
else:
	(start, end) = ((1, 0), (1, 1))
	(value, x_increment, y_increment, in_use) = (2, True, False, 0)
	count = 0
	while True:
		count += 1
		if on([start, end], point):
			break
		start = end
		x = end[0] + (value if x_increment else 0) * (-1 if value % 2 == 0 else 1)
		y = end[1] + (value if y_increment else 0) * (-1 if value % 2 == 0 else 1)
		end = (x, y)
		in_use += 1
		if in_use == 2:
			value += 1
			in_use = 0
		x_increment = not x_increment
		y_increment = not y_increment
	print(count)


(x, y) = map(int, input().split())
z = max(abs(x), abs(y))
c = (z - 1) * 4
if x == z and y > -z + 1 and (y <= z):
	c += 1
elif x < z and x >= -z and (y == z):
	c += 2
elif x == -z and y < z and (y >= -z):
	c += 3
elif y == -z and x > -z and (x <= z + 1):
	c += 4
print(max(0, c))


x = 0
y = 0
(nx, ny) = map(int, input().split())
if (nx == 0 or nx == 1) and ny == 0:
	print(0)
else:
	x = 1
	turn = 0
	flag = 0
	while True:
		turn = turn + 1
		while y != x:
			y = y + 1
			if x == nx and y == ny:
				flag = 1
				break
		if flag == 1:
			break
		k = x * -1
		turn = turn + 1
		while x != k:
			x = x - 1
			if x == nx and y == ny:
				flag = 1
				break
		if flag == 1:
			break
		turn = turn + 1
		while y != x:
			y = y - 1
			if x == nx and y == ny:
				flag = 1
				break
		if flag == 1:
			break
		k = x * -1 + 1
		turn = turn + 1
		while x != k:
			x = x + 1
			if x == nx and y == ny:
				flag = 1
				break
		if flag == 1:
			break
	print(turn)


(x, y) = input().split()
(x, y) = (int(x), int(y))
if x == y == 0:
	print(0)
if y >= -x and y > x:
	print(4 * y - 2)
if x + y < 0 and y >= x:
	print(4 * abs(x) - 1)
if y < x and x + y <= 1:
	print(4 * abs(y))
if x + y > 1 and y <= x:
	print(4 * x - 3)


(x, y) = map(int, input().split())
(a, b, cnt) = (0, 0, 0)
if x == y == 0:
	print(0)
	exit()
while 1:
	while a + b != 1:
		a += 1
		if a == x and b == y:
			print(cnt)
			exit()
	cnt += 1
	while a - b != 0:
		b += 1
		if a == x and b == y:
			print(cnt)
			exit()
	cnt += 1
	while a + b != 0:
		a -= 1
		if a == x and b == y:
			print(cnt)
			exit()
	cnt += 1
	while a != b:
		b -= 1
		if a == x and b == y:
			print(cnt)
			exit()
	cnt += 1


(x, y) = [int(y) for y in input().split()]
ans = 0
if (x == 0 or x == 1) and y == 0:
	print(0)
else:
	ans += 4 * max(abs(x), abs(y))
	if x >= 0 and y <= 0:
		if abs(y) == x - 1:
			ans -= 4
		if abs(y) < x - 1:
			ans -= 3
	elif x >= 0 and y >= 0:
		if y <= x:
			ans -= 3
		else:
			ans -= 2
	elif x < 0 and y >= 0:
		if y >= abs(x):
			ans -= 2
		else:
			ans -= 1
	elif abs(y) <= abs(x):
		ans -= 1
	print(ans)


import math as mt
import sys, string, bisect
input = sys.stdin.readline
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())

def dist(x, y, c, d):
	return (x - c) ** 2 + (y - d) ** 2

def circle(x1, y1, x2, y2, r1, r2):
	distSq = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ** 0.5
	if distSq + r2 <= r1:
		return True
	else:
		return False
(x, y) = M()
d = defaultdict(int)
(a, b) = (0, 0)
d[0, 0] = 0
l = 1
c = 0
while l < 405:
	for t in range(l):
		d[a + 1, b] = c
		a += 1
	c += 1
	for t in range(l):
		d[a, b + 1] = c
		b += 1
	c += 1
	l += 1
	for t in range(l):
		d[a - 1, b] = c
		a -= 1
	c += 1
	for t in range(l):
		d[a, b - 1] = c
		b -= 1
	c += 1
	l += 1
print(d[x, y])