This page is still under construction.

Parts of this page are still being built. What you see may change.

Beehive

Time limit2sMemory limit512 MB

Summary
Given two cell indices in an infinite hexagonal beehive numbered by distance from cell 1, find the grid distance between those cells.
Level

Medium5 of 10

Topics
Math, Geometry, Implementation
Solved
No attempts yet

Problem

An infinite beehive extends as in the figure. Two cells are adjacent exactly when they share a side. A path of length kk from cell c0c_0 to cell ckc_k is a sequence of cells c0,c1,…,ckc_0, c_1, \dots, c_k such that cic_i and ci+1c_{i+1} are adjacent for every ii with 0≤i<k0 \le i < k. The distance between two cells is the length of a shortest path from one to the other.

Each cell of the beehive carries one positive integer index, as the figure shows. A cell farther from cell 1 gets a larger index, and among the cells at the same distance from cell 1 the index grows from left to right. Some cells at the same distance share a column. In the leftmost such column the lower cell comes first, and in the rightmost such column the upper cell comes first. The cells at one distance are therefore numbered by starting at the bottom left cell, climbing that column, crossing the top, and coming down to the bottom right cell. Every positive integer is the index of exactly one cell.

Given the indices of two cells, compute the distance between them.

Input

The input holds several test cases. Each test case is one line with two cell indices ii and jj separated by a single space (1≤i,j≤1041 \le i, j \le 10^4). The last line is 0 0, and that line is not a test case.

Output

For each test case, print the distance between the two given cells on its own line.

Examples7

  1. Example 1

    Input
    8 4
    11 12
    365 365
    0 0
    
    Expected output
    2
    5
    0
    
  2. Example 2

    Input
    1 1
    1 2
    1 3
    1 4
    2 3
    3 4
    2 4
    0 0
    
    Expected output
    0
    1
    1
    1
    1
    1
    2
    
  3. Example 3

    Input
    5 11
    5 6
    10 11
    6 10
    5 8
    8 11
    0 0
    
    Expected output
    4
    1
    1
    4
    3
    3
    
  4. Example 4

    Input
    12 13
    19 20
    12 20
    13 19
    12 16
    16 20
    0 0
    
    Expected output
    1
    1
    6
    6
    4
    4
    
  5. Example 5

    Input
    20 21
    21 33
    21 27
    27 33
    21 1
    33 1
    0 0
    
    Expected output
    7
    8
    6
    6
    4
    4
    
  6. Example 6

    Input
    1 10000
    10000 10000
    10000 1
    9999 10000
    1 9999
    0 0
    
    Expected output
    81
    0
    81
    1
    81
    
  7. Example 7

    Input
    1 1
    0 0
    
    Expected output
    0