Time limit
2s
Memory limit
128 MB
Inside a house tiled with hexagons, a monkey and a dog use different ways to identify the same location.
The dog writes a location as a coordinate (x, y). This is a staggered row coordinate system where y increases by 1 for each row upward. Convert it to axial coordinates (q, r) as follows.
q = x - ceil(y / 2), r = y
The monkey assigns number 1 to the center position (0, 0). Then it numbers positions ring by ring for
k = max(|q|, |r|, |q + r|)
in the order k = 1, 2, 3, .... For a ring with k >= 1, the first number is placed at axial coordinate (0, k), and that value is 3k(k - 1) + 2. From there, the monkey moves along the same ring in the direction order (-1, 0), (0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), assigning consecutive numbers. The final move back to (0, k) is not counted again because that position has already been numbered.
Given the safe's coordinate in the dog's system, output the number the monkey uses for the same location.
The first line contains two integers x and y, the coordinate of the safe.
Each integer is between 0 and 1,000,000, inclusive.
Output the number assigned to the same location in the monkey's numbering system.